deque对象的默认构造
deque <int> deqInt; //一个存放int的deque容器。
deque <float> deq Float; //一个存放float的deque容器。
deque <string> deq String; //一个存放string的deque容器。
...
//尖括号内还可以设置指针类型或自定义类型。
deque末尾的添加移除操作
deque<int>deqInt;
deqInt.push_back(1);
deqInt.push_back(3);
deqInt.push_back(5);
deqInt.push_back(7);
deqInt.push_back(9);
deqInt.pop_front();
deqInt.pop_front();
deqInt.push_front(11);
deqInt.push_front(13);
deqInt.pop_back();
deqInt.pop_back();
//deqInt { 13,11,5}
deque的数据存取
deque<int>deqInt;
deqInt.push_back(1);
deqInt.push_back(3);
deqInt.push_back(5);
deqInt.push_back(7);
deqInt.push_back(9);
intiA = deqInt.at(0); //1
intiB = deqInt[1]; //3
deqInt.at(0)= 99; //99
deqInt[1]= 88; //88
intiFront = deqInt.front(); //99
intiBack = deqInt.back(); //9
deqInt.front()= 77; //77
deqInt.back()= 66; //66
deque与迭代器
deque<int> deqInt;
deqInt.push_back(1);
deqInt.push_back(3);
deqInt.push_back(5);
deqInt.push_back(7);
deqInt.push_back(9);
for(deque<int>::iterator it=deqInt.begin(); it!=deqInt.end(); ++it)
{
cout<< *it;
cout<< "";
}
//1 3 5 7 9
for(deque<int>::reverse_iterator rit=deqInt.rbegin(); rit!=deqInt.rend();++rit)
{
cout<< *rit;
cout<< "";
}
//97 5 3 1
deque对象的带参数构造
deque<int> deqIntA;
deqIntA.push_back(1);
deqIntA.push_back(3);
deqIntA.push_back(5);
deqIntA.push_back(7);
deqIntA.push_back(9);
deque<int>deqIntB(deqIntA.begin(),deqIntA.end()); //13 5 7 9
deque<int>deqIntC(5,8); //88 8 8 8
deque<int>deqIntD(deqIntA); //13 5 7 9
deque的赋值
deque<int> deqIntA,deqIntB,deqIntC,deqIntD;
deqIntA.push_back(1);
deqIntA.push_back(3);
deqIntA.push_back(5);
deqIntA.push_back(7);
deqIntA.push_back(9);
deqIntB.assign(deqIntA.begin(),deqIntA.end()); // 1 3 5 7 9
deqIntC.assign(5,8); //8 8 88 8
deqIntD= deqIntA; //13 5 7 9
deqIntC.swap(deqIntD); //互换
deque的大小
deque<int>deqIntA;
deqIntA.push_back(1);
deqIntA.push_back(3);
deqIntA.push_back(5);
intiSize = deqIntA.size(); //3
if(!deqIntA.empty())
{
deqIntA.resize(5); //1 3 5 0 0
deqIntA.resize(7,1); //1 3 5 0 0 1 1
deqIntA.resize(2); //1 3
}
deque的插入
deque<int>deqA;
deque<int>deqB;
deqA.push_back(1);
deqA.push_back(3);
deqA.push_back(5);
deqA.push_back(7);
deqA.push_back(9);
deqB.push_back(2);
deqB.push_back(4);
deqB.push_back(6);
deqB.push_back(8);
deqA.insert(deqA.begin(),11); //{11, 1, 3, 5, 7, 9}
deqA.insert(deqA.begin()+1,2,33); //{11,33,33,1,3,5,7,9}
deqA.insert(deqA.begin(), deqB.begin() , deqB.end() ); //{2,4,6,8,11,33,33,1,3,5,7,9}
deque的删除
删除区间内的元素
deqInt是用deque<int>声明的容器,现已包含按顺序的1,3,5,6,9元素。
deque<int>::iteratoritBegin=deqInt.begin()+1;
deque<int>::iteratoritEnd=deqInt.begin()+3;
deqInt.erase(itBegin,itEnd);
//此时容器deqInt包含按顺序的1,6,9三个元素。
假设 deqInt 包含1,3,2,3,3,3,4,3,5,3,删除容器中等于3的元素
for(deque<int>::iteratorit=deqInt.being(); it!=deqInt.end(); ) //小括号里不需写 ++it
{
if(*it == 3)
{
it = deqInt.erase(it); //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。
//此时,不执行 ++it;
}
else
{
++it;
}
}
//删除deqInt的所有元素
deqInt.clear(); //容器为空
queue对象的默认构造
queue对象的默认构造形式:queue<T> queT; 如:
queue<int> queInt; //一个存放int的queue容器。
queue<float> queFloat; //一个存放float的queue容器。
queue<string> queString; //一个存放string的queue容器。
...
//尖括号内还可以设置指针类型或自定义类型。
queue的push()与pop()方法
queue<int> queInt;
queInt.push(1);queInt.push(3);
queInt.push(5);queInt.push(7);
queInt.push(9);queInt.pop();
queInt.pop();
此时queInt存放的元素是5,7,9
queue对象的拷贝构造与赋值
queue<int>queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
queue<int>queIntB(queIntA); //拷贝构造
queue<int>queIntC;
queIntC= queIntA; //赋值
queue的数据存取
queue<int>queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
intiFront = queIntA.front(); //1
intiBack = queIntA.back(); //9
queIntA.front()= 11; //11
queIntA.back()= 19; //19
queue的大小
queue<int>queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
if(!queIntA.empty())
{
intiSize = queIntA.size(); //5
}