博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
deque学习之迭代器begin,cbegin,end,cend,rbegin,crbegin,rend,crend
阅读量:2193 次
发布时间:2019-05-02

本文共 1685 字,大约阅读时间需要 5 分钟。

本篇学习deque的迭代器操作:

begin:返回指向起始的迭代器

cbegin:返回指向起始的迭代器

end:返回指向末尾的迭代器

cend:返回指向末尾的迭代器

rbegin:返回指向起始的逆向迭代器

crbegin:返回指向起始的逆向迭代器

rend:返回指向末尾的逆向迭代器

crend:返回指向末尾的逆向迭代器

代码实现

#include 
#include
using namespace std;void getIterator(){ deque
deque1 = {12, 32, 34 ,53, 56, 76, 78}; deque
::iterator it1 = deque1.begin(); while (it1 != deque1.end()) { cout << *it1 << "\t"; ++it1; } cout << endl; auto begin = deque1.begin(); auto last = deque1.end() - 1;//end()是最后一个元素的下一个位置,所以这里要向前移一个位置 *begin = 55; cout << "begin c= " << *begin << " end = " << *last << endl; auto it2 = deque1.begin(); while (it2 != deque1.end()) { cout << *it2 << "\t"; ++it2; } cout << endl; auto cbegin = deque1.cbegin(); auto cend = deque1.cend() - 1; //*cbegin = 88;//cbegin返回的是一个常量迭代器,不能修改他的值 cout << "cbegin = " << *cbegin << " cend = " << *cend << endl; cout << endl; auto rbegin = deque1.rbegin(); auto rend = deque1.rend() - 1;//rend返回的是容器第一个元素的前一个位置 cout << "rbegin = " << *rbegin << " rend = " << *rend << endl; *rbegin = 98;//修改最后一个元素的值 auto it3 = deque1.begin(); while (it3 != deque1.end()) { cout << *it3 << "\t"; ++it3; } cout << endl; auto crbegin = deque1.crbegin(); auto crend = deque1.crend() - 1; //*crbegin = 73;//crbegin返回最后一个常量迭代器,不能修改 cout << "crbegin = " << *crbegin << " crend = " << *crend << endl; cout << endl;}int main(){ getIterator(); cout << endl; cout << "Hello World!" << endl; return 0;}

运行结果:

cbegin, cend, crbegin, crend返回的是常量迭代器,不能通过迭代器修改vector元素的值。这4个函数是C++11里新增加的功能。

参考:

 

转载地址:http://gfiub.baihongyu.com/

你可能感兴趣的文章
【LEETCODE】67-Add Binary
查看>>
【LEETCODE】7-Reverse Integer
查看>>
【LEETCODE】165-Compare Version Numbers
查看>>
【LEETCODE】299-Bulls and Cows
查看>>
【LEETCODE】223-Rectangle Area
查看>>
【LEETCODE】12-Integer to Roman
查看>>
【学习方法】如何分析源代码
查看>>
【LEETCODE】61- Rotate List [Python]
查看>>
【LEETCODE】143- Reorder List [Python]
查看>>
【LEETCODE】82- Remove Duplicates from Sorted List II [Python]
查看>>
【LEETCODE】86- Partition List [Python]
查看>>
【LEETCODE】147- Insertion Sort List [Python]
查看>>
【算法】- 动态规划的编织艺术
查看>>
用 TensorFlow 让你的机器人唱首原创给你听
查看>>
对比学习用 Keras 搭建 CNN RNN 等常用神经网络
查看>>
深度学习的主要应用举例
查看>>
word2vec 模型思想和代码实现
查看>>
怎样做情感分析
查看>>
用深度神经网络处理NER命名实体识别问题
查看>>
用 RNN 训练语言模型生成文本
查看>>