STL中deque
以下学习一下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;
}
输出为:
STL中deque的更多相关文章
- STL中deque 解析
一.deque的中控器 deque是连续空间(至少逻辑上看来如此),连续线性空间总令我们联想到array或vector.array无法成长,vector虽可成长,却只能向尾端成长,而且其所谓的成长原是 ...
- 【转】STL中vector、list、deque和map的区别
1.vector 向量 相当于一个数组 在内存中分配一块连续的内容空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capacity()函数 ...
- STL中vector、list、deque和map的区别
1 vector 向量 相当于一个数组 在内存中分配一块连续的内存空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capac ...
- 深入了解STL中set与hash_set,hash表基础
一,set和hash_set简介 在STL中,set是以红黑树(RB-Tree)作为底层数据结构的,hash_set是以哈希表(Hash table)作为底层数据结构的.set可以在时间复杂度为O(l ...
- C++ STL中Map的按Key排序和按Value排序
map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...
- STL中的单向队列queue
转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...
- STL中的stack(堆栈)
转载:http://blog.csdn.net/morewindows/article/details/6950881 栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(F ...
- C++ STL中迭代器失效的问题
my_container.erase(iter); 其中my_container是STL的某种容器,iter是指向这个容器中某个元素的迭代器.如果不是在for,while循环中,这种方式删除元素没有问 ...
- STL之deque双向队列
deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,提供随机访问,deque在接口上和vector非常相似,下面列出deque的常用成员函数: Table 6.9. C ...
随机推荐
- MysqlHelp
using System.Configuration;using MySql.Data: public class MySqlHelp { //链接字符串 private static string ...
- 2013年末、2014年初合辑——关于c语言的进阶学习
太过于慵懒了,一个多月没有来自己的园子播种了.还是给自己找找借口吧,十二月末备战期末考试也是自己没心情码文字的理由吧,一月份理所当然地进入考试周,回家后做了个小手术也是客观上让自己不能静下心来回顾知识 ...
- 求1+2+...+n
题目:求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C). 程序很简单,就看想到想不到了.悲剧,我属于后者... 算 ...
- (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
原文 (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows She ...
- HDU 3350 #define is unsafe
题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,其中MAX使用宏定义. 题解:注意一个规律,对于MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b ...
- HDU 1267 下沙的沙子有几粒?
题解:利用卡特兰数的几何意义,题目就可以转化为一个棋盘格,可以向下走或是向右走,但是不可以逾越对角线,就可以了. #include <cstdio> #include <iostre ...
- mySQL中replace的用法
MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函数方面能有所启迪 mysql replace实例说明: ...
- poj1144 Network【tarjan求割点】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4319585.html ---by 墨染之樱花 [题目链接]http://poj.org/p ...
- Week 5a - Mouse input and more lists----learning notes
pyton 程序内容的颠倒,运用 [](列表) def reverse_string(s): """Returns the reversal of the given s ...
- Dojo实现Tabs页报错(三)
用Dojo实现tab页的过程中,没有引用“on.js”,但是firebug调试时一直提示如下错误: on.js源码如下: define(["./has!dom-addeventlistene ...