[转]STL之deque容器详解
Deque 容器
deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容。deque容器类与vector类似,支持随机访问和快速插入删除,它在容器中某一位置上的操作所花费的是线性时间。与vector不同的是,deque还支持从开始端插入数据:push_front()。
使用deque容器之前必须加上<deque>头文件:#include<deuqe>;
deque属于std命名域的内容,因此需要通过命名限定:using std::deque;也可以直接使用全局的命名空间方式:using namespace std;
构造函数
deque<Elem> c 创建一个空的deque
deque<Elem> c1(c2) 复制一个deque。
deque<Elem> c(n) 创建一个deque,含有n个数据,数据均已缺省构造产生。
deque<Elem> c(n, elem) 创建一个含有n个elem拷贝的deque。
deque<Elem> c(beg,end) 创建一个以[beg;end)区间的deque。
~deque<Elem>() 销毁所有数据,释放内存。
成员函数
c.begin()返回指向第一个元素的迭代器
c.end()返回指向最后一个元素下一个位置的迭代器
1 deque<int> d {1,2,3,4,5};
2 deque<int>::iterator it;
3 for(it=d.begin();it!=d.end();it++){
4 cout << *it << " ";
5 }
6 cout << endl;
c.rbegin()返回指向反向队列的第一个元素的迭代器(即原队列的最后一个元素)
c.rend()返回指向反向队列的最后一个元素的下一个位置(即原队列的第一个元素的前一个位置)
1 deque<int> d {1,2,3,4,5};
2 deque<int>::reverse_iterator it;
3 for(it=d.rbegin();it!=d.rend();it++){
4 cout << *it << " ";
5 }
6 cout << endl;
operator=赋值运算符重载

1 deque<int> d1 {1,2,3,4,5},d2;
2 d2 = d1;
3 deque<int>::iterator it;
4 for(it=d2.begin();it!=d2.end();it++){
5 cout << *it << " ";
6 }
7 cout << endl;

c.assign(n,num)将n个num拷贝复制到容器c
c.assign(beg,end)将[beg,end)区间的数据拷贝复制到容器c

1 deque<int> d1 {1,2,3,4,5},d2;
2 d2.assign(2, 8);
3 deque<int>::iterator it;
4 cout << "d2.assign(n,num):";
5 for(it=d2.begin();it!=d2.end();it++){
6 cout << *it << " ";
7 }
8 d2.assign(d1.begin(), d1.begin()+3);
9 cout << "d2.assign(beg,end):";
10 for(it=d2.begin();it!=d2.end();it++){
11 cout << *it << " ";
12 }
13 cout << endl;

c.at(pos)返回索引为pos的位置的元素,会执行边界检查,如果越界抛出out_of_range异常
1 deque<int> d {1,2,3,4,5};
2 cout << "d.at(pos):" << d.at(2);
3 return 0;
c.operator[]下标运算符重载
1 deque<int> d {1,2,3,4,5};
2 cout << "d[2]:" << d[2];
3 return 0;
c.empty()判断c容器是否为空

1 deque<int> d {1,2,3,4,5};
2 if(!d.empty()){
3 cout << "d is not empty!" << endl;
4 }else{
5 cout << "d is empty!" << endl;
6 }
7 return 0;

c.front()返回c容器的第一个元素
c.back()返回c容器的最后一个元素
1 deque<int> d {1,2,3,4,5};
2 if(!d.empty()){
3 cout << "d.front():" << d.front() << endl;
4 cout << "d.back(): " << d.back() << endl;
5 }
c.size()返回c容器中实际拥有的元素个数
1 deque<int> d {1,2,3,4,5};
2 cout << "d.size():" << d.size() << endl;
3 return 0;
c.max_size()返回c容器可能存放元素的最大数量
1 deque<int> d {1,2,3,4,5};
2 cout << "d.max_size():" << d.max_size() << endl;
3 return 0;
c.clear()清除c容器中拥有的所有元素

1 deque<int> d {1,2,3,4,5};
2 deque<int>::iterator it;
3 cout << "clear before:" ;
4 for(it=d.begin();it!=d.end();it++){
5 cout << *it << " ";
6 }
7 cout << endl;
8 d.clear();
9 cout << "clear after:" ;
10 for(it=d.begin();it!=d.end();it++){
11 cout << *it << " ";
12 }
13 cout << endl;

c.insert(pos,num)在pos位置插入元素num
c.insert(pos,n,num)在pos位置插入n个元素num
c.insert(pos,beg,end)在pos位置插入区间为[beg,end)的元素

1 deque<int> d {1,2,3,4,5};
2 deque<int>::iterator it;
3 cout << "insert before:" ;
4 for(it=d.begin();it!=d.end();it++){
5 cout << *it << " ";
6 }
7 cout << endl;
8 d.insert(d.end(),22);
9 d.insert(d.end(), 3,88);
10 int a[5] = {1,2,3,4,5};
11 d.insert(d.begin(),a,a+3);
12 cout << "insert after:" ;
13 for(it=d.begin();it!=d.end();it++){
14 cout << *it << " ";
15 }
16 cout << endl;

c.erase(pos)删除pos位置的元素c.erase(beg,end)删除区间为[beg,end)的元素
c.erase(beg,end)删除区间为[beg,end)之间的元素

1 deque<int> d {1,2,3,4,5};
2 d.erase(d.begin());
3 deque<int>::iterator it;
4 cout << "erase(pos) after:" ;
5 for(it=d.begin();it!=d.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9 d.erase(d.begin(), d.begin()+3);
10 cout << "erase(beg,end) after:" ;
11 for(it=d.begin();it!=d.end();it++){
12 cout << *it << " ";
13 }
14 cout << endl;

c.push_back(num)在末尾位置插入元素
c.pop_back()删除末尾位置的元素
c.push_front(num)在开头位置插入元素
c.pop_front()删除开头位置的元素

1 deque<int> d {1,2,3,4,5};
2 d.push_back(10);
3 deque<int>::iterator it;
4 cout << "push_back(num):" ;
5 for(it=d.begin();it!=d.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9
10 d.pop_back();
11 cout << "pop_back(num):" ;
12 for(it=d.begin();it!=d.end();it++){
13 cout << *it << " ";
14 }
15 cout << endl;
16
17 d.push_front(10);
18 cout << "push_front(num):" ;
19 for(it=d.begin();it!=d.end();it++){
20 cout << *it << " ";
21 }
22 cout << endl;
23
24 d.pop_front();
25 cout << "pop_front(num):" ;
26 for(it=d.begin();it!=d.end();it++){
27 cout << *it << " ";
28 }
29 cout << endl;
30 return 0;

c.resize(num)从新定义容器的大小

1 deque<int> d {1,2,3,4,5};
2 cout << "d.size():" << d.size() << endl;
3 d.resize(d.size()+5);
4 cout << "d.resize() after:" << d.size() <<endl;
5 deque<int>::iterator it;
6 cout << "resize() after:" ;
7 for(it=d.begin();it!=d.end();it++){
8 cout << *it << " ";
9 }
10 cout << endl;

c1.swap(c2)交换容器c1,c2;
swap(c1,c2)同上。

1 deque<int> d1 {1,2,3,4,5},d2,d3;
2 d1.swap(d2);
3 deque<int>::iterator it;
4 cout << "d1 swap after:" ;
5 for(it=d1.begin();it!=d1.end();it++){
6 cout << *it << " ";
7 }
8 cout << endl;
9 cout << "d2 swap after:" ;
10 for(it=d2.begin();it!=d2.end();it++){
11 cout << *it << " ";
12 }
13 cout << endl;
14
15 swap(d3,d2);
16 cout << "d3 swap after:" ;
17 for(it=d3.begin();it!=d3.end();it++){
18 cout << *it << " ";
19 }
20 cout << endl;

重载运算符
operator==
operator!=
operator<
operator<=
operator>
operator>=
[转]STL之deque容器详解的更多相关文章
- 2.4 C++STL deque容器详解
文章目录 2.4.1 引入 2.4.2 代码示例 2.4.3 代码运行结果 2.4.4 具体案例 总结 2.4.1 引入 deque容器类比vector容器来学习. deque为双向开口容器,见下图. ...
- 跟我一起学STL(2)——vector容器详解
一.引言 在上一个专题中,我们介绍了STL中的六大组件,其中容器组件是大多数人经常使用的,因为STL容器是把运用最广的数据结构实现出来,所以我们写应用程序时运用的比较多.然而容器又可以序列式容器和关联 ...
- STL:deque用法详解
deque函数: deque容器为一个给定类型的元素进行线性处理,像向量一样,它能够快速地随机访问任一个元素,并且能够高效地插入和删除容器的尾部元素.但它又与vector不同,deque支持高效插入和 ...
- STL之deque用法详解
C++ Deque(双向队列): Deque是一种优化了的.对序列两端元素进行添加和删除操作的基本序列容器.它允许较为快速地随机访问,但它不像vector 把所有的对象保存在一块连续的内存块,而是采用 ...
- STL之vector容器详解
vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...
- [转]STL之vector容器详解
vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...
- [转]STL之list容器详解
List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容.实际上,list容器就是一个双向链表,可以高效地进行插入删除元素. 使用list容 ...
- 2.8 C++STL set/multiset容器详解
文章目录 2.8.1 引入 2.8.2 代码示例 2.8.3 代码运行结果 2.8.4 对组pair的补充 代码实例 运行结果 总结 2.8.1 引入 set/multiset容器概念 set和mul ...
- 2.9 C++STL map/multimap容器详解
文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...
随机推荐
- Sigmoid Function
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51734189 Sigmodi 函数是一 ...
- 兼容IE8 addEventListener、removeEventListener 函数
//兼容bind函数 if(!Function.prototype.bind){ Function.prototype.bind = function(){ if(typeof this !== 'f ...
- HDU1914(稳定婚姻)
http://acm.hdu.edu.cn/showproblem.php?pid=1914 思路:Gale-Shapley算法.算法过程是男士不停地求婚,女士不停地拒绝.在每一轮中,每个尚未订婚的男 ...
- 洛谷 P2683 小岛
P2683 小岛 题目背景 西伯利亚北部的寒地,坐落着由 N 个小岛组成的岛屿群,我们把这些小岛依次编号为 1 到 N . 题目描述 起初,岛屿之间没有任何的航线.后来随着交通的发展,逐渐出现了一些连 ...
- spring mvc参数校验
一.在SringMVC中使用 使用注解 1.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-validator-4.2.0.Fina ...
- 使用jconsole监控远程JVM
使用jconsole监控远程JVM 学习了:https://www.linuxidc.com/Linux/2015-02/113420.htm https://www.cnblogs.com/thin ...
- python基础学习之02 序列
#encoding=utf-8 import math a=[1,23,4,5,6] print a a[1:1]=[2,3,5] print a a a[1]='a' print a a[1]={1 ...
- Python3基础(八) 模块
在程序中定义函数可以实现代码重用.但当你的代码逐渐变得庞大时,你可能想要把它分割成几个文件,以便能够更简单地维护.同时,你希望在一个文件中写的代码能够被其他文件所重用,这时我们应该使用模块(modul ...
- 《C# 6.0 本质论》 阅读笔记
<C# 6.0 本质论> 阅读笔记 阅读笔记不是讲述这本书的内容,只是提取了其中一部分我认为比较重要或者还没有掌握的知识,所以如果有错误或者模糊之处,请指正,谢谢! 对于C# 6.0才 ...
- VIM学习笔记 比较文件(diff)
比较 可以从命令行调用以下命令,来打开两个文件进行比较: vim -d file1 file2 如果已经打开了文件file1,那么可以在Vim中用以下命令,再打开另一个文件file2进行比较: :di ...