STL學習系列三:Deque容器

來源:互聯網
上載者:User

標籤:

1.Deque簡介
  • deque是“double-ended queue”的縮寫,和vector一樣都是STL的容器,deque是雙端數組,而vector是單端的。
  • deque在介面上和vector非常相似,在許多操作的地方可以直接替換。
  • deque可以隨機存取元素(支援索引值直接存取, 用[]操作符或at()方法,這個等下會詳講)。
  • deque頭部和尾部添加或移除元素都非常快速。但是在中部安插元素或移除元素比較費時。
2.deque對象的預設構造

deque採用模板類實現,deque對象的預設構造形式:deque<T> deq; 

deque <int> deqInt;            //一個存放int的deque容器。

deque <float> deq Float;     //一個存放float的deque容器。

3.deque對象的帶參數構造

  • deque(beg,end);    //建構函式將[beg, end)區間中的元素拷貝給本身。注意該區間是左閉右開的區間。
  • deque(n,elem);   //建構函式將n個elem拷貝給本身。
  • deque(const deque  &deq);  //拷貝建構函式。
#include<iostream>using namespace std;#include <deque>void objPlay3(){    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 }int main(){    objPlay3();    return 0;}

 

4.deque的賦值
  • deque.assign(beg,end);    //將[beg, end)區間中的資料拷貝賦值給本身。注意該區間是左閉右開的區間。
  • deque.assign(n,elem);  //將n個elem拷貝賦值給本身。
  • deque& operator=(const deque &deq);    //重載等號操作符
  • deque.swap(deq);  // 將vec與本身的元素互換
void objPlay4(){    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);                         //C 和 D互換}

 

5.deque的大小

  • deque.size();                     //返回容器中元素的個數。
  • deque.empty();                 //判斷容器是否為空白。
  • deque.resize(num);           //重新指定容器的長度為num,若容器變長,則以預設值填充新位置。如果容器變短,則末尾超出容器長度的元素被刪除。
  • deque.resize(num, elem);  //重新指定容器的長度為num,若容器變長,則以elem值填充新位置。如果容器變短,則末尾超出容器長度的元素被刪除。
 

void objPlay5()
{
  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
  }

}

 

6.deque末尾的添加移除操作

  • deque.push_back(elem);   //在容器尾部添加一個資料
  • deque.push_front(elem);  //在容器頭部插入一個資料
  • deque.pop_back();                  //刪除容器最後一個資料
  • deque.pop_front();             //刪除容器第一個資料

 

7.deque的資料存取
  • deque.at(idx);  //返回索引idx所指的資料,如果idx越界,拋出out_of_range。
  • deque[idx];  //返回索引idx所指的資料,如果idx越界,不拋出異常,直接出錯。
  • deque.front();   //返回第一個資料。
  • deque.back();  //返回最後一個資料

8.deque的插入

  • deque.insert(pos,elem);   //在pos位置插入一個elem元素的拷貝,返回新資料的位置。
  • deque.insert(pos,n,elem);   //在pos位置插入n個elem資料,無傳回值。
  • deque.insert(pos,beg,end);   //在pos位置插入[beg,end)區間的資料,無傳回值。
9.deque的刪除
  • deque.clear();    //移除容器的所有資料
  • deque.erase(beg,end);  //刪除[beg,end)區間的資料,返回下一個資料的位置。
  • deque.erase(pos);    //刪除pos位置的資料,返回下一個資料的位置。

STL學習系列三:Deque容器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.