34 lines
863 B
C++
34 lines
863 B
C++
|
// 【场景】双端队列操作
|
|||
|
// 编写一个程序,演示双端队列(deque)的以下操作: 1)在队列的前面插入一个元素。 2)在队列的后面插入一个元素。 3)从队列的前面删除一个元素。 4)从队列的后面删除一个元素。
|
|||
|
#include <iostream>
|
|||
|
#include <deque>
|
|||
|
|
|||
|
using namespace std;
|
|||
|
|
|||
|
template <typename T>
|
|||
|
void print(deque<T> &dq)
|
|||
|
{
|
|||
|
// cout << "******************" << endl;
|
|||
|
typename deque<T>::iterator it = dq.begin();
|
|||
|
for (; it != dq.end(); it++)
|
|||
|
{
|
|||
|
cout << *it << endl;
|
|||
|
}
|
|||
|
cout << "------------------" << endl;
|
|||
|
}
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
deque<string> dq;
|
|||
|
dq.push_front("push_front");
|
|||
|
dq.push_back("push_back");
|
|||
|
cout << "删除前: " << endl;
|
|||
|
print(dq);
|
|||
|
|
|||
|
dq.pop_front();
|
|||
|
dq.pop_back();
|
|||
|
cout << "删除后: " << endl;
|
|||
|
print(dq);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|