STL Deque 容器
STL Deque 容器
?
?
?
?
Deque簡介?
? ? ? ? deque是“double-ended queue”的縮寫,和vector一樣都是STL的容器,deque是雙 端的,而vector是單端的。
? ? ? ??deque在接口上和vector非常相似,在許多操作的地方可以直接替換。
? ? ? ??deque可以隨機存取元素(支持索引值直接存取, 用[]操作符或at()方法,這個等下會詳講)。?
? ? ? ??deque頭部和尾部添加或移除元素都非常快速。但是在中部安插元素或移除元素比較費時。?
? ? ? ??#include <deque>
?
deque對象的默認構(gòu)造
deque采用模板類實現(xiàn),
? ? ? ? ?deque對象的默認構(gòu)造形式:
? ? ? ? ?deque<T> deqT
示例:
deque <int> deqInt;
//一個存放int的deque容器。
deque <float> deq Float;
//一個存放float的deque容器。
deque <string> deq String;
//一個存放string的deque容器。
...
//尖括號內(nèi)還可以設置指針類型或自定義類型。
?
deque對象的帶參數(shù)構(gòu)造
理論知識
deque(beg,end)
構(gòu)造函數(shù)將[beg, end)區(qū)間中的元素拷貝給本身。注意該區(qū)間是左閉右開的區(qū)間。
deque(n,elem)
構(gòu)造函數(shù)將n個elem拷貝給本身。
deque(const deque &deq)
拷貝構(gòu)造函數(shù)。
?
示例:
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
?
deque頭尾的添加移除操作
理論知識:
deque.push_back(elem)
在容器尾部添加一個數(shù)據(jù)
deque.push_front(elem)
在容器頭部插入一個數(shù)據(jù)
deque.pop_back()
刪除容器最后一個數(shù)據(jù)
deque.pop_front()
刪除容器第一個數(shù)據(jù)
?
示例:
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的數(shù)據(jù)存取
理論知識:
deque.at(idx)
返回索引idx所指的數(shù)據(jù),如果idx越界,拋出out_of_range。
deque[idx]
返回索引idx所指的數(shù)據(jù),如果idx越界,不 拋出異常,直接出錯。
deque.front()
返回第一個數(shù)據(jù)。
deque.back()
返回最后一個數(shù)據(jù)
?
示例:
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
?
deque的大小
理論知識
deque.size()
返回容器中元素的個數(shù)
deque.empty()
判斷容器是否為空
deque.resize(num)
重新指定容器的長度為num,若容器變長 ,則以默認值填充新位置。如果容器變短 ,則末尾超出容器長度的元素被刪除。
deque.resize(num, elem)
重新指定容器的長度為num,若容 器變長,則以elem值填充新位置。如果容器變短,則末尾超出容器長度的元素被刪除。
?
示例:
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
}
?
deque與迭代器
理論知識
deque.begin()
返回容器中第一個元素的迭代器。
deque.end()
返回容器中最后一個元素之后的迭代器。
deque.rbegin()
返回容器中倒數(shù)第一個元素的迭代器。
deque.rend()
返回容器中倒數(shù)最后一個元素之后的迭代器。
?
示例:
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
deque的賦值
理論知識
deque.assign(beg,end)
將[beg, end)區(qū)間中的數(shù)據(jù)拷貝賦值給本身。注意該區(qū)間是左閉右開的區(qū)間。
deque.assign(n,elem)
將n個elem拷貝賦值給本身。
deque& operator=(const deque &deq)
重載等號操作符
deque.swap(deq)
將deq與本身的元素互換
?
示例:?
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); //互換
?
deque的插入
理論知識
deq.insert(pos,elem)
在pos位置插入一個elem元素的拷貝,返回新數(shù)據(jù)的位置。
deq.insert(pos,n,elem)
在pos位置插入n個elem數(shù)據(jù),無返回值。
deq.insert(pos,beg,end)
在pos位置插入[beg,end)區(qū)間的數(shù)據(jù),無返回值。
?
示例:
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的刪除
理論知識
deq.clear()
移除容器的所有數(shù)據(jù)
deq.erase(beg,end)
刪除[beg,end)區(qū)間的數(shù)據(jù),返回下一個數(shù)據(jù)的位置。
deq.erase(pos)
刪除pos位置的數(shù)據(jù),返回下一個數(shù)據(jù)的位置。
示例:
刪除區(qū)間內(nèi)的元素
deqInt是用deque<int>聲明的容器,現(xiàn)已包含按順序的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); //以迭代器為參數(shù),刪除元素3,并把數(shù)據(jù)刪除后的下一個元素位置返回給迭代器。
? ? ? ?? ? ? ?//此時,不執(zhí)行 ++it;
? ? ? ?}
? ? ? ?else
? ? ? ?{
? ? ? ?? ? ? ?++it;
? ? ? ?}
}
//刪除deqInt的所有元素
deqInt.clear(); //容器為空
?
轉(zhuǎn)載于:https://www.cnblogs.com/lsgxeva/p/7790336.html
總結(jié)
以上是生活随笔為你收集整理的STL Deque 容器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: border绘制三角形
- 下一篇: SQL中的long text