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;
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;
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;
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;
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的更多相关文章

  1. STL deque详解

    英文原文:http://www.codeproject.com/Articles/5425/An-In-Depth-Study-of-the-STL-Deque-Container 绪言 这篇文章深入 ...

  2. STL Deque 容器

    STL Deque 容器 Deque简介 deque是“double-ended queue”的缩写,和vector一样都是STL的容器,deque是双 端的,而vector是单端的.         ...

  3. 浅谈C++ STL deque 容器

    浅谈C++ STL deque 容器 本篇随笔简单介绍一下\(C++STL\)中\(deque\)容器的使用方法及常见使用技巧. deque容器的概念 \(deque\)的意义是:双端队列.队列是我们 ...

  4. [STL]deque和stack、queue

    怎么说呢,deque是一种双向开口的连续线性空间,至少逻辑上看上去是这样.然而事实上却没有那么简单,准确来说deque其实是一种分段连续空间,因此其实现以及各种操作比vector复杂的多. 一.deq ...

  5. STL deque用法

    Deque 容器 deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容.deque容器类与vector类似,支持随机访问和快速插入删除,它在容器中 ...

  6. STL~Deque简介

    转自百度经验deque简介 deque是双向开口的连续性存储空间.虽说是连续性存储空间,但这种连续性只是表面上的,实际上它的内存是动态分配的,它在堆上分配了一块一块的动态储存区,每一块动态存储去本身是 ...

  7. STL --> deque双向队列

    deque简介 deque是双向开口的连续性存储空间.虽说是连续性存储空间,但这种连续性只是表面上的,实际上它的内存是动态分配的,它在堆上分配了一块一块的动态储存区,每一块动态存储去本身是连续的,de ...

  8. C++STL deque

    deque双端数组 deque<int> dq; deque<int>::iterator it; dq.push_back();//尾部插入元素 dq.push_front( ...

  9. 计蒜客 A2232.程序设计:蒜厂年会-单调队列(双端队列(STL deque)实现)滑窗维护最小前缀和

    程序设计:蒜厂年会 问答问题反馈 只看题面 16.79% 1000ms 262144K   在蒜厂年会上有一个抽奖,在一个环形的桌子上,有 nn 个纸团,每个纸团上写一个数字,表示你可以获得多少蒜币. ...

随机推荐

  1. php中require、require_once、include、include_once类库重复引入效率问题详解

    首先我详细说下这四个引入函数 include() 与require() 的功能相同 唯一不同:require()不管是否被执行,只要存在,php在执行前都会预引入,include()则是执行到该语句时 ...

  2. JavaWeb学习笔记五 会话技术Cookie&Session

    什么是会话技术? 例如网站的购物系统,用户将购买的商品信息存储到哪里?因为Http协议是无状态的,也就是说每个客户访问服务器端资源时,服务器并不知道该客户端是谁,所以需要会话技术识别客户端的状态.会话 ...

  3. JAVA入门——Generic/泛型

    在台科大的第二次JAVA作业,老师课上讲的内容是泛型. 泛型(generic),泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以 ...

  4. alpha冲刺第十天

    一.合照 二.项目燃尽图 三.项目进展 之前卡住的注册无法插入数据的问题解决 可以呈现多条数据内容了,首页文章内容呈现,问答界面问题内容呈现 四.明日规划 很多没有完善的,后面还是要继续整改 五.问题 ...

  5. Python 实现队列

    操作 Queue() 创建一个空的队列 enqueue(item) 往队列中添加一个item元素 dequeue() 从队列头部删除一个元素 is_empty() 判断一个队列是否为空 size() ...

  6. 申请JetBrains学生免费注册码

    1.申请.edu.*后缀的邮箱 从某个知乎用户上面得到了两个可以申请的后缀edu的邮箱 上海交通大学校友统一身份认证:https://register.alumni.sjtu.edu.cn/alumn ...

  7. Xdebug的优点!php代码开发

    可以跟踪函数,知道那个函数出错,之前只是输出一点调试信息,只是哪一行错了,并且是白色 如果是死循环,debug会对死循环判断一百次的循环,并且会给出每一次循环的时间,内存,和函数名,位置.根据时间可以 ...

  8. React 深入系列2:组件分类

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列2:组件分类 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助大家加 ...

  9. Struts2 之值栈

    值栈(ValueStack) http://www.cnblogs.com/bgzyy/p/8639893.html 这是我的有关 struts2 的第一篇文章,对于里面我们说到的一个 struts2 ...

  10. Linux背景知识(1)RedHat和Centos

    Redhat有收费的商业版和免费的开源版,商业版的业内称之为RHEL(Red Hat Enterprise Linux)系列, 而这个CentOS(Community ENTerprise Opera ...