deque是双端队列,与vector非常相似,是顺序容器,不同的是,deque可以在数组开头和末尾插入和删除数据。支持快速随机访问。
#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;
int main()
{
deque<int> a;
// 在末尾插入数据
a.push_back(3);
a.push_back(4);
a.push_back(5);
// 在开头插入数据
a.push_front(2);
a.push_front(1);
a.push_front(0);
// 以数组方式输出
for (size_t n = 0; n < a.size(); ++n)
cout << "a[" << n << "] = " << a[n] << endl;
cout << endl;
a.pop_back();
a.pop_front();
// 以迭代器方式输出
deque<int>::iterator iter;
for (iter = a.begin(); iter < a.end(); ++iter)
{
// 计算数组下标,distance包含在algorithm中
int index = distance(a.begin(), iter);
cout << "a[" << index << "] = " << *iter << endl;
}
system(