C++ Deque cbegin()函数返回一个常数迭代器,该迭代器指向deque集合的第一个元素。可以像begin()函数返回的迭代器一样递增或递减该迭代器。如果集合为空,则返回的迭代器将等于cend()。
const_iterator cbegin();
它返回一个指向集合开头的常量迭代器。
让我们看一个简单的例子
#include <iostream> #include<deque> using namespace std; int main() { deque<string> fruit={"mango","apple","banana","kiwi"}; deque<string>::const_iterator itr; itr=fruit.cbegin(); cout<<*itr; return 0; }
输出:
mango
在此示例中,cbegin()函数将常量迭代器返回到集合的开头。
让我们看一个简单的例子
#include <iostream> #include<deque> using namespace std; int main() { deque<int> k={100,200,300,400,500}; deque<int>::const_iterator itr; itr=k.cbegin()+3; cout<<*itr; return 0; }
输出:
400
在此示例中,cbegin()函数递增3。因此,它返回第4个元素的迭代器。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)