deque.pop_front(); //删除容器第一个数据
<span style="white-space:pre"> </span>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}
<span style="white-space:pre"> </span>deque<int> deqInt; deqInt.push_back(1); deqInt.push_back(3); deqInt.push_back(5); deqInt.push_back(7); deqInt.push_back(9); int iA = deqInt.at(0); //1 int iB = deqInt[1]; //3 deqInt.at(0) = 99; //99 deqInt[1] = 88; //88 int iFront = deqInt.front(); //99 int iBack = deqInt.back(); //9 deqInt.front() = 77; //77 deqInt.back() = 66; //66
<span style="white-space:pre"> </span>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 << ""; } //9 7 5 3 1
<span style="white-space:pre"> </span>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()); //1 3 5 7 9 deque<int> deqIntC(5,8); //8 8 8 8 8 deque<int> deqIntD(deqIntA); //1 3 5 7 9
<span style="white-space:pre"> </span>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 8 8 8 deqIntD = deqIntA; //1 3 5 7 9 deqIntC.swap(deqIntD); //互换
<span style="white-space:pre"> </span>deque<int> deqIntA; deqIntA.push_back(1); deqIntA.push_back(3); deqIntA.push_back(5); int iSize = 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 }
<span style="white-space:pre"> </span>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}
删除区间内的元素 deqInt是用deque<int>声明的容器,现已包含按顺序的1,3,5,6,9元素。 deque<int>::iterator itBegin=deqInt.begin()+1; deque<int>::iterator itEnd=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>::iterator it=deqInt.being(); it!=deqInt.end(); ) //小括号里不需写 ++it { if(*it == 3) { it = deqInt.erase(it); //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。 //此时,不执行 ++it; } else { ++it; } } //删除deqInt的所有元素 deqInt.clear(); //容器为空
demo
#include <iostream> #include <cstdio> #include <deque> #include <algorithm> using namespace std; void printDeque(deque<int> &d) { for (deque<int>::iterator it = d.begin(); it != d.end(); it++) { cout << *it << ' '; } cout << endl; } void dequeInit() { 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); printDeque(d1); // -55 -33 -11 1 3 5 cout << "front element: " << d1.front() << endl; // front element: -55 cout << "back element: " << d1.back() << endl; // back element: 5 d1.pop_front(); d1.pop_back(); printDeque(d1); // -33 -11 1 3 // 查找1在d1中的位置 deque<int>::iterator it = find(d1.begin(), d1.end(), 1); if (it != d1.end()) { cout << "position of 1: " << distance(d1.begin(), it) << endl; } else { cout << "no 1\n"; } // position of 1: 2 } int main() { dequeInit(); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zyq522376829/article/details/46801973