本文介绍STL中的双端数组deque的用法示例。deque 是一种双端数组,可以从头部和尾部插入元素和弹出元素。下面是示例代码。
#include "iostream"
using namespace std;
#include "deque"
#include "algorithm"
void printD(deque<int> &d)
{
for (deque<int>::iterator it = d.begin();it!=d.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void main31()
{
deque<int> d1;
d1.push_back(1);// 从尾部插入
d1.push_back(3);
d1.push_back(5);
d1.push_front(-11);// 从头部插入
d1.push_front(-33);
d1.push_front(-55);
printD(d1);
cout << "头部元素:" << d1.front() << endl;
cout << "尾部元素:" << d1.back() << endl;
d1.pop_front();// 从头部弹出
d1.pop_back();// 从尾部弹出
printD(d1);
// 查找-33在数组中的下标的值
deque<int>::iterator it = find(d1.begin(),d1.end(),-33);
if (it != d1.end())
{
cout << "下标为:" << distance(d1.begin(),it) << endl;
}
else
{
cout << "没有找到!" << endl;
}
}
void main()
{
main31();
cout << "hello..."<< endl;
system("pause");
}