deque容器詳解

Deque 容器

deque容器是C++標準模版庫(STL,Standard Template Library)中的部分內容。deque容器類與vector類似,支持隨機訪問和快速插入刪除,它在容器中某一位置上的操作所花費的是線性時間。與vector不同的是,deque還支持從開始端插入數據:push_front()。

使用deque容器之前必須加上<deque>頭文件:#include<deuqe>;

       deque屬於std命名域的內容,因此需要通過命名限定:using std::deque;也可以直接使用全局的命名空間方式:using namespace std;

 

構造函數

  deque<Elem> c 創建一個空的deque

  deque<Elem> c1(c2) 複製一個deque。

  deque<Elem> c(n) 創建一個deque,含有n個數據,數據均已缺省構造產生。

  deque<Elem> c(n, elem) 創建一個含有n個elem拷貝的deque。

  deque<Elem> c(beg,end) 創建一個以[beg;end)區間的deque。

  ~deque<Elem>() 銷燬所有數據,釋放內存。

 

成員函數

c.begin()返回指向第一個元素的迭代器

c.end()返回指向最後一個元素下一個位置的迭代器

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()返回指向反向隊列的第一個元素的迭代器(即原隊列的最後一個元素)

c.rend()返回指向反向隊列的最後一個元素的下一個位置(即原隊列的第一個元素的前一個位置)

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=賦值運算符重載

複製代碼
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個num拷貝複製到容器c

c.assign(beg,end)將[beg,end)區間的數據拷貝複製到容器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的位置的元素,會執行邊界檢查,如果越界拋出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[]下標運算符重載

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容器的第一個元素

c.back()返回c容器的最後一個元素

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容器中實際擁有的元素個數

1     deque<int> d {1,2,3,4,5};
2     cout << "d.size():" << d.size() << endl;
3     return 0;

c.max_size()返回c容器可能存放元素的最大數量

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個元素num

c.insert(pos,beg,end)在pos位置插入區間爲[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)刪除區間爲[beg,end)的元素

c.erase(beg,end)刪除區間爲[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)在開頭位置插入元素

c.pop_front()刪除開頭位置的元素

複製代碼
 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;
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章