/* strdeque.cpp
所谓deque,是"double-ended queue"的缩写;
它是一种动态数组形式,可以向两端发展,在尾部和头部插入元素非常迅速;
在中间插入元素比较费时,因为需要移动其它元素;(No)
双端队列容器,在序列的两端放置和删除元素是高效的;
而vector只是在序列末尾插入才是高效的。
*/
#include <iostream>
#include <deque>
using namespace std;
int main(void)
{
//push_front();
//push_back();
//pop_front();
//pop_back();
deque<int> d;
d.push_back();
d.push_back();
d.push_back();
d.push_front();
; i < d.size(); i++)
{
cout << "d[" << i << "] = " << d[i] << endl;
}
d.pop_front();
d[;
; i < d.size(); i++)
{
cout << "d[" << i << "] = " << d[i] << endl;
}
//迭代器遍历
typedef deque<int>::iterator PITER;
for (PITER piter = d.begin(); piter != d.end(); piter++)
{
cout << *piter << endl;
}
//大小度量
cout << d.size() << endl;
d.resize();
cout << d.size() << endl;
cout << d.max_size() << endl;
//返回函数
//begin front rbegin 下同
//end back rend
PITER pIter = d.begin();
cout << "The first element is " << *pIter << endl;
*pIter = ;
pIter = d.begin();
cout << "The first element is " << *pIter << endl;
int &nRet = d.front();
cout << "The first element is " << nRet << endl;
typedef deque<int>::reverse_iterator PREVITER;
PREVITER pRevIter = d.rbegin();
cout << "The last element is " << *pRevIter << endl;
//判断是否为空
if (d.empty())
{
cout << "empty" << endl;
}
//deque访问
deque<char> dchar;
dchar.push_back('A');
dchar.push_back('B');
dchar.push_back('C');
dchar.push_back('D');
; i < dchar.size(); i++)
{
cout << dchar.at(i) << " ";
}
cout << endl;
//deque重置技术
deque<char> dchar2;
dchar2.push_back('a');
dchar2.push_back('b');
dchar2.push_back('c');
dchar2.push_back('d');
dchar.assign(dchar2.begin(), dchar2.end());
; i < dchar.size(); i++)
{
cout << dchar.at(i) << " ";
}
cout << endl;
//容器内容交换
);
; i < d1.size(); i++)
{
cout << d1.at(i) << " ";
}
cout << endl;
deque<);
d1.swap(d2);
; i < d1.size(); i++)
{
cout << d1.at(i) << " ";
}
cout << endl;
//deque插入和删除技术
);
; i < dInsert.size(); i++)
{
cout << dInsert.at(i) << " ";
}
cout << endl;
dInsert.insert(dInsert.end(), );
; i < dInsert.size(); i++)
{
//cout << dInsert.at(i) << " ";
cout << dInsert[i] << " ";
}
cout << endl;
//erase操作
//注意:尽量少用erase(pos)和erase(beg,end)
/*
deque<int> dErase(3, 7);
dErase.erase(dErase.end());
for (PITER p = dErase.begin(); p != dErase.end(); p++)
{
cout << *p << " ";
}
cout << endl;
*/
//clear操作
);
for (PITER p = dClear.begin(); p != dClear.end(); p++)
{
cout << *p << " ";
}
cout << endl;
dClear.clear();
if (dClear.empty())
{
cout << "empty it!" << endl;
}
//除此之外还有关系运算符
// == > >= < <=等等 操作省略
cin.get();
;
}