转自:https://www.cnblogs.com/grhyxzc/p/5074061.html
需要包括头文件<deque>;
定义:
deque<数据类型> 变量名;
Eg:
deque<int> que;//定义了一个整型的双端队列;
基本操作(红色标识为常用操作):
#include<iostream>
#include<cstdio>
#include<deque>
using namespace std;
int main()
{
deque <int> que;
que.push_back(1);
que.push_back(2);
que.push_back(3);
cout<<"基本操作:"<<endl;
cout<<"队列遍历:"<<endl;
deque<int>::iterator ator;
for (ator=que.begin();ator!=que.end();ator++)
cout<<*ator<<endl;
int a;
cout<<"队首插入元素:";
cin>>a;
que.push_front(a);
cout<<"队列遍历:"<<endl;
for (ator=que.begin();ator!=que.end();ator++)
cout<<*ator<<endl;
cout<<"队尾插入元素:";
cin>>a;
que.push_back(a);
cout<<"队列遍历:"<<endl;
for (ator=que.begin();ator!=que.end();ator++)
cout<<*ator<<endl;
cout<<"中间插入元素:";
cin>>a;
que.insert(que.begin()+1/*迭代器位置,根据要求更改*/,1,a);//第一个位置为迭代器位置,该程序插入的为在首位置之后、第一个元素之前插入。元素标号从第0个开始。
cout<<"队列遍历:"<<endl;
for (ator=que.begin();ator!=que.end();ator++)
cout<<*ator<<endl;
cout<<"删除队首元素:"<<endl;
que.pop_front();
cout<<"队列遍历:"<<endl;
for (ator=que.begin();ator!=que.end();ator++)
cout<<*ator<<endl;
cout<<"删除队尾元素:"<<endl;
que.pop_back();
cout<<"队列遍历:"<<endl;
for (ator=que.begin();ator!=que.end();ator++)
cout<<*ator<<endl;
cout<<"队列元素个数:"<<que.size()<<endl;
cout<<"队列清0:"<<endl;
que.clear();
cout<<"队列元素个数:"<<que.size()<<endl;
return 0;
}