STL 之 deque容器详解
Deque 容器
deque容器是C++標(biāo)準(zhǔn)模版庫(kù)(STL,Standard Template Library)中的部分內(nèi)容。deque容器類(lèi)與vector類(lèi)似,支持隨機(jī)訪問(wèn)和快速插入刪除,它在容器中某一位置上的操作所花費(fèi)的是線性時(shí)間。與vector不同的是,deque還支持從開(kāi)始端插入數(shù)據(jù):push_front()。
使用deque容器之前必須加上<deque>頭文件:#include<deuqe>;
?? ? ? deque屬于std命名域的內(nèi)容,因此需要通過(guò)命名限定:using std::deque;也可以直接使用全局的命名空間方式:using namespace std;
?
構(gòu)造函數(shù)
deque<Elem> c 創(chuàng)建一個(gè)空的deque
deque<Elem> c1(c2) 復(fù)制一個(gè)deque。
deque<Elem> c(n) 創(chuàng)建一個(gè)deque,含有n個(gè)數(shù)據(jù),數(shù)據(jù)均已缺省構(gòu)造產(chǎn)生。
deque<Elem> c(n, elem) 創(chuàng)建一個(gè)含有n個(gè)elem拷貝的deque。
deque<Elem> c(beg,end) 創(chuàng)建一個(gè)以[beg;end)區(qū)間的deque。
~deque<Elem>() 銷(xiāo)毀所有數(shù)據(jù),釋放內(nèi)存。
?
成員函數(shù)
c.begin()返回指向第一個(gè)元素的迭代器
c.end()返回指向最后一個(gè)元素下一個(gè)位置的迭代器
1 deque<int> d {1,2,3,4,5}; 2 deque<int>::iterator it; 3 for(it=d.begin();it!=d.end();it++){ 4 cout << *it << " "; 5 } 6 cout << endl;c.rbegin()返回指向反向隊(duì)列的第一個(gè)元素的迭代器(即原隊(duì)列的最后一個(gè)元素)
c.rend()返回指向反向隊(duì)列的最后一個(gè)元素的下一個(gè)位置(即原隊(duì)列的第一個(gè)元素的前一個(gè)位置)
1 deque<int> d {1,2,3,4,5}; 2 deque<int>::reverse_iterator it; 3 for(it=d.rbegin();it!=d.rend();it++){ 4 cout << *it << " "; 5 } 6 cout << endl;operator=賦值運(yùn)算符重載
1 deque<int> d1 {1,2,3,4,5},d2; 2 d2 = d1; 3 deque<int>::iterator it; 4 for(it=d2.begin();it!=d2.end();it++){ 5 cout << *it << " "; 6 } 7 cout << endl;c.assign(n,num)將n個(gè)num拷貝復(fù)制到容器c
c.assign(beg,end)將[beg,end)區(qū)間的數(shù)據(jù)拷貝復(fù)制到容器c
1 deque<int> d1 {1,2,3,4,5},d2; 2 d2.assign(2, 8); 3 deque<int>::iterator it; 4 cout << "d2.assign(n,num):"; 5 for(it=d2.begin();it!=d2.end();it++){ 6 cout << *it << " "; 7 } 8 d2.assign(d1.begin(), d1.begin()+3); 9 cout << "d2.assign(beg,end):"; 10 for(it=d2.begin();it!=d2.end();it++){ 11 cout << *it << " "; 12 } 13 cout << endl;c.at(pos)返回索引為pos的位置的元素,會(huì)執(zhí)行邊界檢查,如果越界拋出out_of_range異常
1 deque<int> d {1,2,3,4,5}; 2 cout << "d.at(pos):" << d.at(2); 3 return 0;c.operator[]下標(biāo)運(yùn)算符重載
1 deque<int> d {1,2,3,4,5}; 2 cout << "d[2]:" << d[2]; 3 return 0;c.empty()判斷c容器是否為空
1 deque<int> d {1,2,3,4,5}; 2 if(!d.empty()){ 3 cout << "d is not empty!" << endl; 4 }else{ 5 cout << "d is empty!" << endl; 6 } 7 return 0;c.front()返回c容器的第一個(gè)元素
c.back()返回c容器的最后一個(gè)元素
1 deque<int> d {1,2,3,4,5}; 2 if(!d.empty()){ 3 cout << "d.front():" << d.front() << endl; 4 cout << "d.back(): " << d.back() << endl; 5 }c.size()返回c容器中實(shí)際擁有的元素個(gè)數(shù)
1 deque<int> d {1,2,3,4,5}; 2 cout << "d.size():" << d.size() << endl; 3 return 0;c.max_size()返回c容器可能存放元素的最大數(shù)量
1 deque<int> d {1,2,3,4,5}; 2 cout << "d.max_size():" << d.max_size() << endl; 3 return 0;c.clear()清除c容器中擁有的所有元素
1 deque<int> d {1,2,3,4,5}; 2 deque<int>::iterator it; 3 cout << "clear before:" ; 4 for(it=d.begin();it!=d.end();it++){ 5 cout << *it << " "; 6 } 7 cout << endl; 8 d.clear(); 9 cout << "clear after:" ; 10 for(it=d.begin();it!=d.end();it++){ 11 cout << *it << " "; 12 } 13 cout << endl;c.insert(pos,num)在pos位置插入元素num
c.insert(pos,n,num)在pos位置插入n個(gè)元素num
c.insert(pos,beg,end)在pos位置插入?yún)^(qū)間為[beg,end)的元素
1 deque<int> d {1,2,3,4,5}; 2 deque<int>::iterator it; 3 cout << "insert before:" ; 4 for(it=d.begin();it!=d.end();it++){ 5 cout << *it << " "; 6 } 7 cout << endl; 8 d.insert(d.end(),22); 9 d.insert(d.end(), 3,88); 10 int a[5] = {1,2,3,4,5}; 11 d.insert(d.begin(),a,a+3); 12 cout << "insert after:" ; 13 for(it=d.begin();it!=d.end();it++){ 14 cout << *it << " "; 15 } 16 cout << endl;c.erase(pos)刪除pos位置的元素c.erase(beg,end)刪除區(qū)間為[beg,end)的元素
c.erase(beg,end)刪除區(qū)間為[beg,end)之間的元素
1 deque<int> d {1,2,3,4,5}; 2 d.erase(d.begin()); 3 deque<int>::iterator it; 4 cout << "erase(pos) after:" ; 5 for(it=d.begin();it!=d.end();it++){ 6 cout << *it << " "; 7 } 8 cout << endl; 9 d.erase(d.begin(), d.begin()+3); 10 cout << "erase(beg,end) after:" ; 11 for(it=d.begin();it!=d.end();it++){ 12 cout << *it << " "; 13 } 14 cout << endl;c.push_back(num)在末尾位置插入元素
c.pop_back()刪除末尾位置的元素
c.push_front(num)在開(kāi)頭位置插入元素
c.pop_front()刪除開(kāi)頭位置的元素
1 deque<int> d {1,2,3,4,5}; 2 d.push_back(10); 3 deque<int>::iterator it; 4 cout << "push_back(num):" ; 5 for(it=d.begin();it!=d.end();it++){ 6 cout << *it << " "; 7 } 8 cout << endl; 9 10 d.pop_back(); 11 cout << "pop_back(num):" ; 12 for(it=d.begin();it!=d.end();it++){ 13 cout << *it << " "; 14 } 15 cout << endl; 16 17 d.push_front(10); 18 cout << "push_front(num):" ; 19 for(it=d.begin();it!=d.end();it++){ 20 cout << *it << " "; 21 } 22 cout << endl; 23 24 d.pop_front(); 25 cout << "pop_front(num):" ; 26 for(it=d.begin();it!=d.end();it++){ 27 cout << *it << " "; 28 } 29 cout << endl; 30 return 0;?
c.resize(num)從新定義容器的大小
1 deque<int> d {1,2,3,4,5}; 2 cout << "d.size():" << d.size() << endl; 3 d.resize(d.size()+5); 4 cout << "d.resize() after:" << d.size() <<endl; 5 deque<int>::iterator it; 6 cout << "resize() after:" ; 7 for(it=d.begin();it!=d.end();it++){ 8 cout << *it << " "; 9 } 10 cout << endl;c1.swap(c2)交換容器c1,c2;
swap(c1,c2)同上。
1 deque<int> d1 {1,2,3,4,5},d2,d3; 2 d1.swap(d2); 3 deque<int>::iterator it; 4 cout << "d1 swap after:" ; 5 for(it=d1.begin();it!=d1.end();it++){ 6 cout << *it << " "; 7 } 8 cout << endl; 9 cout << "d2 swap after:" ; 10 for(it=d2.begin();it!=d2.end();it++){ 11 cout << *it << " "; 12 } 13 cout << endl; 14 15 swap(d3,d2); 16 cout << "d3 swap after:" ; 17 for(it=d3.begin();it!=d3.end();it++){ 18 cout << *it << " "; 19 } 20 cout << endl;?
?
重載運(yùn)算符
operator==
operator!=
operator<
operator<=
operator>
operator>=
分類(lèi): STL 容器 好文要頂 關(guān)注我 收藏該文 Sam大叔關(guān)注 - 0
粉絲 - 11 +加關(guān)注 0 0 ? 上一篇:STL之list容器詳解
? 下一篇:單例模式
總結(jié)
以上是生活随笔為你收集整理的STL 之 deque容器详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: STL 之 list 容器详解
- 下一篇: FFT算法的完整DSP实现