STL之双端队列(deque)
2016-03-13 16:19
531 查看
以下内容来自转载:
双端队列是由动态数组实现。头文件:
#include <deque>
构造方法:
deque<type> deq 创建一个没有任何元素的双端队列
deque<type> deq(otherDeq) 用另一个类型相同双端队列初始化该双端队列
deque<type> deq(size) 初始化一个固定size的双端队列
deque<type> deq(n, element) 初始化n个相同元素的双端队列
deque<type> deq(begin,end) 初始化双端队列中的某一段元素,从begin 到 end - 1
双端队列的特有操作(双端对列是顺序容器,顺序容器有容器的共有操作)
deq.assign(n,elem) 赋值n个元素的拷贝给双端队列
deq.assign(beg,end) 赋值一段迭代器的值给双端队列
deq.push_front(elem) 添加一个元素在开头
deq.pop_front() 删除第一个元素
deq.at(index) 取固定位置的元素
deq[index] 取固定位置的元素
deq.front() 返回第一个元素(不检测容器是否为空)
deq.back() 返回最后一个元素(不检测容器是否为空)
示例代码:
#include <deque> #include <iostream> #include <algorithm> #include <iterator> using namespace std; int main() { deque<int> intDqe; ostream_iterator<int> screen(cout, " "); // 从双端队列的尾部添加元素 intDqe.push_back(13); intDqe.push_back(75); intDqe.push_back(28); intDqe.push_back(35); // 输出到控制台 copy(intDqe.begin(),intDqe.end(),screen); cout << endl; // 从双端队列的首部添加元素 intDqe.push_front(0); intDqe.push_back(100); // 输出到控制台 copy(intDqe.begin(),intDqe.end(),screen); cout << endl; // 从首部删除两个元素 intDqe.pop_front(); intDqe.pop_front(); // 输出到控制台 copy(intDqe.begin(),intDqe.end(),screen); cout << endl; // 从尾部删除两个元素 intDqe.pop_back(); intDqe.pop_back(); // 输出到控制台 copy(intDqe.begin(),intDqe.end(),screen); cout << endl; // 构造双端队列的迭代器 deque<int>::iterator deqIt; deqIt = intDqe.begin(); ++deqIt; // 插入元素 intDqe.insert(deqIt,444); // 输出到控制台 copy(intDqe.begin(),intDqe.end(),screen); cout << endl; // 重新赋值,两个相同元素 intDqe.assign(2,45); // 输出到控制台 copy(intDqe.begin(),intDqe.end(),screen); cout << endl; // 从首部和尾部添加元素 intDqe.push_front(-10); intDqe.push_back(-1999); // 输出到控制台 copy(intDqe.begin(),intDqe.end(),screen); cout << endl; return 0; }
输出结果:
13 75 28 35
0 13 75 28 35 100
75 28 35 100
75 28
75 444 28
45 45
-10 45 45 -1999
相关文章推荐
- GUI
- 关于YES、NO和TRUE、FLASE的区别
- 74.Interesting Sequence(有趣的数列)(拓扑排序)
- 74.Interesting Sequence(有趣的数列)(拓扑排序)
- SEQUENCE自增列
- 设置GUI中Label的字体大小
- 用while(true)和continue、break来实现在一个大循环里switch
- STL list和deque
- NSThread、NSOperation/NSOperationQueue、GCD多线程
- Deep learning:四十四(Pylearn2中的Quick-start例子)
- poj-2478 Farey Sequence(dp,欧拉函数)
- iOS8新特性之基于地理位置的消息通知UILocalNotification
- Java中的continue、break和return
- Pop Sequence
- 子线程更新UI
- This Android SDK requires Android Developer Toolkit version 23.0.0 or above
- pintuer e.handler.apply is not a function
- 《iOS Human Interface Guidelines》——Map View
- How to Advertise Android as a Bluetooth LE Peripheral
- java中线程队列BlockingQueue的用法