头文件 #include <deque>
常用方法:
a.push_front(0);//在头部加入数据0
a.push_back(11);//在尾部加入数据11
a.pop_front();//在头部删除数据
a.pop_back();//在尾部删除数据
a.resize(num);//重新指定队列的长度
a.size() //返回容器中实际数据个数
a.max_size() //返回容器中最大数据的数量
测试代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#include <iostream> #include <deque> #include <algorithm> using namespace std; int main() { deque<int> a(10); //创建一个有10个元素双端队列a,初始值都为0 //给deque赋值 for (int i = 0; i < a.size(); i++) { a[i] = i + 1; } //输出deque中的数据 for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } cout << endl; //在头尾加入新数据 cout << "在头部加入数据0:" << endl; a.push_front(0); for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } cout << endl; cout << "在尾部加入数据11:" << endl; a.push_back(11); for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } cout << endl; //在头尾删除数据 cout << "在头部删除数据: " << endl; a.pop_front(); for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } cout << endl; cout << "在尾部删除数据: " << endl; a.pop_back(); for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } cout << endl; //返回容器中实际数据个数 cout << "a.size() = " << a.size() << endl; //重新指定队列的长度 a.resize(11); for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } cout << endl; //判断队列是否为空 cout << "a.empty() = " << a.empty() << endl; //返回容器中最大数据的数量 cout << "a.max_size() = " << a.max_size() << endl; return 0; } |
输入输出:
1 2 3 4 5 6 7 8 9 10
在头部加入数据0:
0 1 2 3 4 5 6 7 8 9 10
在尾部加入数据11:
0 1 2 3 4 5 6 7 8 9 10 11
在头部删除数据:
1 2 3 4 5 6 7 8 9 10 11
在尾部删除数据:
1 2 3 4 5 6 7 8 9 10
a.size() = 10
1 2 3 4 5 6 7 8 9 10 0
a.empty() = 0
a.max_size() = 4611686018427387903