以下学习一下STL中另一种序列容器——deque。

deque表示double-ended queue,即双向队列,deque是通过作为动态数组的方式实现的,这样可以在两端插入元素。因此,deque可以在任何一个方向进行扩展。同时可以在中间插入元素。在开头或结尾处插入元素非常的快,然而在中间插入元素将会比较耗时间,因此需要移动队列中的元素。

定义deque容器的类名为deque。类deque的定义以及deque对象的各种操作函数的实现包含在头文件<deque>中,因此,在程序中使用deque时,程序中必须包含如下语句:

#include <deque>

类deque中包含好几个构造器,因此,当声明一个deque对象时,可以通过各种方式进行初始化。如下表中提供的方式:

除了之前介绍的所有序列容器通用的操作意外,下表还描述了用来管理deque容器的元素的操作。各个语句展示了如何使用某一个特定的函数,其中假设deq是一个deque容器

下例展示了如何在程序中使用deque。

#include <iostream>
#include <deque>
#include <algorithm>
#include <iterator> using namespace std; int main()
{
deque<int> intDeq;
ostream_iterator<int> screen(cout, " "); intDeq.push_back(13);
intDeq.push_back(75);
intDeq.push_back(28);
intDeq.push_back(35); cout << "intDeq: ";
copy(intDeq.begin(), intDeq.end(), screen);
cout << endl; intDeq.push_front(0);
intDeq.push_back(100); cout << "After adding two more "
<< "elements, one at the front " << endl
<< " and one at the back, intDeq: "; copy(intDeq.begin(), intDeq.end(), screen);
cout << endl; intDeq.pop_front();
intDeq.pop_front(); cout << "After removing the first "
<< "two elements, " << endl
<< " intDeq: ";
copy(intDeq.begin(), intDeq.end(), screen);
cout << endl; intDeq.pop_back();
intDeq.pop_back(); cout << "After removing the last "
<< "two elements, " << endl
<< " intDeq: ";
copy(intDeq.begin(), intDeq.end(), screen);
cout << endl; deque<int>::iterator deqIt;
deqIt = intDeq.begin();
++deqIt;
intDeq.insert(deqIt, 666); cout << "After inserting 666, "
<< "intDeq: ";
copy(intDeq.begin(), intDeq.end(), screen);
cout << endl; intDeq.assign(2, 45); cout << "After assigning two "
<< "copies of 45, intDeq: ";
copy(intDeq.begin(), intDeq.end(), screen);
cout << endl; intDeq.push_front(-10);
intDeq.push_front(-999); cout << "After inserting two "
<< "elements, one at the front " << endl
<< " and one at the back, intDeq: ";
copy(intDeq.begin(), intDeq.end(), screen);
cout << endl; return 0;
}

输出为:

最新文章

  1. OpenMP之求和(用section分块完成)
  2. [linux]查看linux下端口占用
  3. 用python requests库写一个人人网相册爬虫
  4. cocos游戏的真正入口,用C++实现的demo版本
  5. Foxit Reader 插件下载
  6. ListView往TreView里面拖拽
  7. Android学习笔记--存储方案(SharedPreference、文件IO)
  8. Keil C51 知识点
  9. VS2010下测试程序性能瓶颈
  10. Android通过HTTP POST带參訪问asp.net网页
  11. AIDL使用以及原理分析
  12. bootstrap table 分页序号递增问题 (转)
  13. Tp5自定义标签
  14. MySQL 0Day漏洞出现 该漏洞可以拿到本地Root权限
  15. 51Nod 1001 数组中和等于K的数对
  16. 使用Akka构建集群(二)
  17. POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)
  18. Java多线程之Lock的使用&lt;转&gt;
  19. 【Linux】使用awk批量杀进程
  20. FPGA学习的一点总结

热门文章

  1. Ajax学习之小结
  2. Spring Annotation vs XML - 示例
  3. ThinkPHP第十九天(Ueditor高亮插件、扩展函数载入load、静态缓存)
  4. PHP新闻系统开发流程
  5. IOS 使用IOS6苹果地图
  6. docker4dotnet
  7. Oracle SQL篇(三)Oracle ROWNUM 与TOP N分析
  8. MySQL DBA成长之路
  9. 没有开发者账号,如何解锁wp8设备
  10. zip文件压缩(转)