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 (3, 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=1; i<coll.size(); ++i) {
           coll[i] = "another " + coll [i];

       }

       //change size to four elements
       coll.resize (4, "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
时间: 2024-11-09 00:13:04

STL之deque双向队列的相关文章

STL系列之一 deque双向队列(转载)

deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,deque在接口上和vector非常相似,下面列出deque的常用成员函数: deque的实现比较复杂,内部会维护一个map(注意!不是STL中的map容器)即一小块连续的空间,该空间中每个元素都是指针,指向另一段(较大的)区域,这个区域称为缓冲区,缓冲区用来保存deque中的数据.因此deque在随机访问和遍历数据会比vector慢.具体的deque实现可以参考<STL源码剖析>,当然此书中使用的SGI ST

C++ Deque(双向队列)

  C++ Deque(双向队列)是一种优化了的.对序列两端元素进行添加和删除操作的基本序列容器.它允许较为快速地随机访问,但它不像vector 把所有的对象保存在一块连续的内存块,而是采用多个连续的存储块,并且在一个映射结构中保存对这些块及其顺序的跟踪.向deque 两端添加或删除元素的开销很小.它不需要重新分配空间,所以向末端增加元素比vector 更有效. 实际上,deque 是对vector 和list 优缺点的结合,它是处于两者之间的一种容器. Deque 的特点: (1) 随机访问方

STL --&gt; deque双向队列

deque简介 deque是双向开口的连续性存储空间.虽说是连续性存储空间,但这种连续性只是表面上的,实际上它的内存是动态分配的,它在堆上分配了一块一块的动态储存区,每一块动态存储去本身是连续的,deque自身的机制把这一块一块的存储区虚拟地连在一起. 它首次插入一个元素,默认会动态分配512字节空间,当这512字节空间用完后,它会再动态分配自己另外的512字节空间,然后虚拟地连在一起.deque 的这种设计使得它具有比vector复杂得多的架构.算法和迭代器设计.它的性能损失比之vector,

deque双向队列

对于双向队列,与队列queue以及vector容器的区别就在于,名字不同,也就是它是双向的,可以从头开始操作,也可以从末尾开始操作. 双向队列的常用方法跟队列queue差不多: 头文件: #include<deque> 函数: 构造/析构 deque<int>q  构造一个空的双向队列 deque<int>q(q1)  构造q,并复制q1 deque<int>q(n)  创建deque,含有n个数据,数据均由缺省构造函数产生 deque<int>

Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)

Counter(计数器) 是一个字典的子类,存储形式同样为字典,其中存储的键为字典的元素,值为元素出现的次数,在使用之前我们需要先导入文件 import collections 初始化一个计数器 import collections # 初始化一个计数器 c = collections.Counter('sldfjoaoaufdlfuaof') print(c) # Counter({'f': 4, 'o': 3, 'a': 3, 'd': 2, 'u': 2, 'l': 2, 'j': 1,

python的collection系列-双向队列和单向队列

单向队列:数据先进先出 双向队列:可进可出 双向队列部分源码: 1 class deque(object): 2 """ 3 deque([iterable[, maxlen]]) --> deque object 4 5 Build an ordered collection with optimized access from its endpoints. 6 """ 7 def append(self, *args, **kwargs

STL之deque用法

deque:双端队列 底层是一个双向链表. 常用的有队列的尾部入队.首部出队. 摘自:http://www.cnblogs.com/liubilan/p/9461141.html deque - 双向队列 1.构造 无参构造: deque<T> a; //<>内自定义数据类型: 带参构造: deque(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身.注意该区间是左闭右开的区间. deque(n,elem); //构造函数将n个elem拷贝给本身. de

Python学习笔记-Day03 -第二部分(双向队列-deque和单向队列Queue)

双向队列 线程安全的双向队列 例 >>> a= collections.deque() >>> a.append(1) >>> a.appendleft(2) >>> a.append(3) >>> a.appendleft(4) >>> a.append(5) >>> a.appendleft(6) >>> a deque([6, 4, 2, 1, 3, 5])

双端队列篇deque SDUT OJ 双向队列

双向队列 Time Limit: 1000MS Memory limit: 65536K 题目描述 想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首:两头都可以做出队,入队的操作. 现在给你一系列的操作,请输出最后队列的状态: 命令格式: LIN X  X表示一个整数,命令代表左边进队操作: RIN X  表示右边进队操作: ROUT LOUT   表示出队操作: 输入 第一行包含一个整数M(M<=10000),表示有M个操作: 以下M行每行包含一条命令: 命令可能不合法