STL之deque双向队列
deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,提供随机访问,deque在接口上和vector非常相似,下面列出deque的常用成员函数:
Table 6.9. Constructors and Destructor of Deques
| Operation | Effect |
| deque<Elem> c | Creates an empty deque without any elements |
| deque<Elem> c1(c2) | Creates a copy of another deque of the same type (all elements are copied) |
| deque<Elem> c(n) | Creates a deque with n elements that are created by the default constructor |
| deque<Elem> c(n,elem) | Creates a deque initialized with n copies of element elem |
| deque<Elem> c(beg,end) | Creates a deque initialized with the elements of the range [beg,end) |
| c.~deque<Elem>() | Destroys all elements and frees the memory |
Table 6.10. Nonmodifying Operations of Deques
| Operation | Effect |
| c.size() | Returns the actual number of elements |
| c.empty () | Returns whether the container is empty (equivalent to size()==0, but might be faster) |
| c.max_size() | Returns the maximum number of elements possible |
| c1 == c2 | Returns whether c1 is equal to c2 |
| c1 != c2 | Returns whether c1 is not equal to c2 (equivalent to ! (c1==c2)) |
| c1 < c2 | Returns whether c1 is less than c2 |
| c1 > c2 | Returns whether c1 is greater than c2 (equivalent to c2<c1) |
| c1 <= c2 | Returns whether c1 is less than or equal to c2 (equivalent to ! (c2<c1) ) |
| c1 >= c2 | Returns whether c1 is greater than or equal to c2 (equivalent to ! (c1<c2) ) |
| c.at(idx) | Returns the element with index idx (throws range error exception if idx is out of range) |
| c[idx] | Returns the element with index idx (no range checking) |
| c.front() | Returns the first element (no check whether a first element exists) |
| c.back() | Returns the last element (no check whether a last element exists) |
| c.begin() | Returns a random access iterator for the first element |
| c.end() | Returns a random access iterator for the position after the last element |
| c.rbegin() | Returns a reverse iterator for the first element of a reverse iteration |
| c.rend() | Returns a reverse iterator for the position after the last element of a reverse iteration |
Table 6.11. Modifying Operations of Deques
| Operation | Effect |
| c1 = c2 | Assigns all elements of c2 to c1 |
| c.assign (n,elem) | Assigns n copies of element elem |
| c.assign (beg,end) | Assigns the elements of the range [beg,end) |
| c1.swap(c2) | Swaps the data of c1 and c2 |
| swap(c1,c2) | Same (as global function) |
| c.insert (pos,elem) | Inserts at iterator position pos a copy of elem and returns the position of the new element |
| c. insert (pos,n,elem) | Inserts at iterator position pos n copies of elem (returns nothing) |
| c.insert (pos,beg,end) | Inserts at iterator position pos a copy of all elements of the range [beg,end) (returns nothing) |
| c.push_back (elem) | Appends a copy of elem at the end |
| c.pop_back() | Removes the last element (does not return it) |
| c.push_front (elem) | Inserts a copy of elem at the beginning |
| c.pop_front() | Removes the first element (does not return it) |
| c.erase(pos) | Removes the element at iterator position pos and returns the position of the next element |
| c.erase (beg,end) | Removes all elements of the range [beg,end) and returns the position of the next element |
| c. resize (num) | Changes the number of elements to num (if size () grows, new elements are created by their default constructor) |
| c.resize (num, elem) | Changes the number of elements to num (if size () grows, new elements are copies of elem) |
| c.clear() | Removes all elements (makes the container empty) |
deque的实现比较复杂,内部会维护一个map(注意!不是STL中的map容器)即一小块连续的空间,该空间中每个元素都是指针,指向另一段(较大的)区域,这个区域称为缓冲区,缓冲区用来保存deque中的数据。因此deque在随机访问和遍历数据会比vector慢。具体的deque实现可以参考《STL源码剖析》,当然此书中使用的SGI STL与VS2008所使用的PJ STL的实现方法还是有区别的。下面给出了deque的结构图:

由于篇幅问题,deque的实现细节就不再深入了,下面给出deque的使用范例:
// cont/deque1. cpp #include <iostream>
#include <deque>
#include <string>
#include <algorithm>
using namespace std; int main()
{ //create empty deque of strings
deque<string> coll; //insert several elements
coll.assign (, string("string"));
coll.push_back ("last string");
coll.push_front ("first string"); //print elements separated by newlines
copy (coll.begin(), coll.end(),
ostream_iterator<string>(cout,"\n"));
cout << endl; //remove first and last element
coll.pop_front();
coll.pop_back(); //insert ''another'' into every element but the first
for (int i=; i<coll.size(); ++i) {
coll[i] = "another " + coll [i]; } //change size to four elements
coll.resize (, "resized string"); //print elements separated by newlines
copy (coll.begin(), coll.end(),
ostream_iterator<string>(cout,"\n")); }
The program has the following output:
first string
string
string
string
last string string
another string
another string
resized string
STL之deque双向队列的更多相关文章
- deque双向队列(转)
deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,deque在接口上和vector非常相似,下面列出deque的常用成员函数: deque的实现比较复杂,内部会维 ...
- deque(双向队列)基本用法
deque(双向队列)基本用法 阅读体验:https://zybuluo.com/Junlier/note/1297030 简单介绍 就是可以两头插元素,两头删元素的数据结构 那么具体的STL操作(只 ...
- collection系列用法-deque双向队列
deque双向队列 Deque可以从两端添加和删除元素.常用的结构,是它的简化版本. Deque支持序列的常用操作,现在举一个简单例子,你会发现其实跟平成的list没啥区别: import colle ...
- 【转载】deque双向队列
继vector和queue之后,又发现一个很好用的东西. 本篇转载自http://blog.csdn.net/morewindows/article/details/6946811 deque双向队列 ...
- C++ Deque(双向队列)
C++ Deque(双向队列)是一种优化了的.对序列两端元素进行添加和删除操作的基本序列容器.它允许较为快速地随机访问,但它不像vector 把所有的对象保存在一块连续的内存块,而是采用多个连续的 ...
- STL --> deque双向队列
deque简介 deque是双向开口的连续性存储空间.虽说是连续性存储空间,但这种连续性只是表面上的,实际上它的内存是动态分配的,它在堆上分配了一块一块的动态储存区,每一块动态存储去本身是连续的,de ...
- deque双向队列
对于双向队列,与队列queue以及vector容器的区别就在于,名字不同,也就是它是双向的,可以从头开始操作,也可以从末尾开始操作. 双向队列的常用方法跟队列queue差不多: 头文件: #inclu ...
- STL之deque(双向队列)
deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,deque在接口上和vector非常相似,下面列出deque的常用成员函数: deque在vector函数的基础上增 ...
- Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)
Counter(计数器) 是一个字典的子类,存储形式同样为字典,其中存储的键为字典的元素,值为元素出现的次数,在使用之前我们需要先导入文件 import collections 初始化一个计数器 im ...
随机推荐
- iOS面试题整理(一)
代码规范 这是一个重点考察项,曾经在微博上发过一个风格纠错题: 也曾在面试时让人当场改过,槽点不少,能够有 10 处以上修改的就基本达到标准了(处女座的人在这方面表现都很优秀 一个区分度很大的面试题 ...
- form表单中的label标签
小伙伴们,你们在前面学习表单各种控件的时候,有没有发现一个标签--label,这一小节就来揭晓它的作用. label标签不会向用户呈现任何特殊效果,它的作用是为鼠标用户改进了可用性.如果你在 labe ...
- 关于typedef int(*lpAddFun)(int, int)
lpAddFun是typedef定义的一个名称 可以用来定义变量 比如 lpAddFun p; 那 p就是 int(*p)(int, int); 首先(*p)说明p是一个指针,(*p)();说明p指向 ...
- 【USACO 3.1.5】联系
[描述] 奶牛们开始对用射电望远镜扫描牧场外的宇宙感兴趣.最近,他们注意到了一种非常奇怪的脉冲调制微波从星系的中央发射出来.他们希望知道电波是否是被某些地外生命发射出来的,还是仅仅是普通的的星星发出的 ...
- 完数c实现
完数,顾名思义,就是一个数如果恰好等于它的因子之和.例如6=1+2+3.编写找出1000以内的所有完数 #include <stdio.h> #include <stdlib.h&g ...
- 给出2n+1个数,其中有2n个数出现过两次,如何用最简便的方法找出里面只出现了一次的那个数(转载)
有2n+1个数,其中有2n个数出现过两次,找出其中只出现一次的数 例如这样一组数3,3,1,2,4,2,5,5,4,其中只有1出现了1次,其他都是出现了2次,如何找出其中的1? 最简便的方法是使用异或 ...
- call和apply
在js中经常会看到call和apply,他们实际上就是用于改变this的上下文 经典例子 function pet(words) { this.words=words; this.speak=func ...
- 移除IOS下按钮的原生样式
写WAP页面的时候 一定要加上这组样式,以避免在IOS下面按钮被系统原生样式影响 input,textarea {outline-style:none;-webkit-appearance:none ...
- ThinkPHP 发送post请求
function post($url, $param=array()){ if(!is_array($param)){ throw new Exception("参数必须为array&quo ...
- 数据结构 --- 线性表学习(php模拟)
线性表:零个或多个数据元素的有限序列(注:以下都是用的整型数据模拟) 一 顺序存储结构(用一段地址连续的存储单元一次存储线性表的数据元素) 1.1 三个属性:存储空间的起始位置:最大存储容量:当前长度 ...