天天看點

STL之list容器詳解

原文位址為: STL之list容器詳解

List 容器

list是C++标準模版庫(STL,Standard Template Library)中的部分内容。實際上,list容器就是一個雙向連結清單,可以高效地進行插入删除元素。

使用list容器之前必須加上<vector>頭檔案:#include<list>;

list屬于std命名域的内容,是以需要通過命名限定:using std::list;也可以直接使用全局的命名空間方式:using namespace std;

構造函數

    list<int> c0; //空連結清單

  list<int> c1(3); //建一個含三個預設值是0的元素的連結清單

  list<int> c2(5,2); //建一個含五個元素的連結清單,值都是2

  list<int> c4(c2); //建一個c2的copy連結清單

  list<int> c5(c1.begin(),c1.end()); c5含c1一個區域的元素[_First, _Last)。

成員函數

c.begin()      傳回指向連結清單第一個元素的疊代器。

c.end()      傳回指向連結清單最後一個元素之後的疊代器。

1     list<int> a1{1,2,3,4,5};
2     list<int>::iterator it;
3     for(it = a1.begin();it!=a1.end();it++){
4         cout << *it << "\t";
5     }
6     cout << endl;      

c.rbegin()      傳回逆向連結清單的第一個元素,即c連結清單的最後一個資料。

c.rend()      傳回逆向連結清單的最後一個元素的下一個位置,即c連結清單的第一個資料再往前的位置。

1     list<int> a1{1,2,3,4,5};
2     list<int>::reverse_iterator it;
3     for(it = a1.rbegin();it!=a1.rend();it++){
4         cout << *it << "\t";
5     }
6     cout << endl;      

operator=      重載指派運算符。

1     list<int> a1 {1,2,3,4,5},a2;
2     a2 = a1;
3     list<int>::iterator it;
4     for(it = a2.begin();it!=a2.end();it++){
5         cout << *it << endl;
6     }      

c.assign(n,num)      将n個num拷貝指派給連結清單c。

c.assign(beg,end)      将[beg,end)區間的元素拷貝指派給連結清單c。

1     int a[5] = {1,2,3,4,5};
 2     list<int> a1;
 3     list<int>::iterator it;
 4     a1.assign(2,10);
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     a1.assign(a,a+5);
10     for(it = a1.begin();it!=a1.end();it++){
11         cout << *it << " ";
12     }
13     cout << endl;      

c.front()      傳回連結清單c的第一個元素。

c.back()      傳回連結清單c的最後一個元素。

1     list<int> a1{1,2,3,4,5};
2     if(!a1.empty()){
3         cout << "the first number is:" << a1.front() << endl;
4         cout << "the last number is:" << a1.back() << endl;
5     }      

c.empty()  判斷連結清單是否為空。

1     list<int> a1{1,2,3,4,5};
2     if(!a1.empty())
3         cout << "a1 is not empty" << endl;
4     else
5         cout << " a1 is empty" << endl;      

c.size()      傳回連結清單c中實際元素的個數。

1     list<int> a1{1,2,3,4,5};
2     cout << a1.size() << endl;      

c.max_size()      傳回連結清單c可能容納的最大元素數量。

1     list<int> a1{1,2,3,4,5};
2     cout << a1.max_size() << endl;      

c.clear()      清除連結清單c中的所有元素。

1     list<int> a1{1,2,3,4,5};
 2     list<int>::iterator it;
 3     cout << "clear before:";
 4     for(it = a1.begin();it!=a1.end();it++){
 5         cout << *it << "\t";
 6     }
 7     cout << endl;
 8     a1.clear();
 9     cout << "clear after:";
10     for(it = a1.begin();it!=a1.end();it++){
11         cout << *it << "\t";
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     list<int> a1{1,2,3,4,5};
 2     list<int>::iterator it;
 3     cout << "insert before:";
 4     for(it = a1.begin();it!=a1.end();it++){
 5         cout << *it << " ";
 6     }
 7     cout << endl;
 8     
 9     a1.insert(a1.begin(),0);
10     cout << "insert(pos,num) after:";
11     for(it = a1.begin();it!=a1.end();it++){
12         cout << *it << " ";
13     }
14     cout << endl;
15     
16     a1.insert(a1.begin(),2,88);
17     cout << "insert(pos,n,num) after:";
18     for(it = a1.begin();it!=a1.end();it++){
19         cout << *it << " ";
20     }
21     cout << endl;
22 
23     int arr[5] = {11,22,33,44,55};
24     a1.insert(a1.begin(),arr,arr+3);
25     cout << "insert(pos,beg,end) after:";
26     for(it = a1.begin();it!=a1.end();it++){
27         cout << *it << " ";
28     }
29     cout << endl;      

c.erase(pos)    删除pos位置的元素。

1     list<int> a1{1,2,3,4,5};
 2     list<int>::iterator it;
 3     cout << "erase before:";
 4     for(it = a1.begin();it!=a1.end();it++){
 5         cout << *it << " ";
 6     }
 7     cout << endl;
 8     a1.erase(a1.begin());
 9     cout << "erase after:";
10     for(it = a1.begin();it!=a1.end();it++){
11         cout << *it << " ";
12     }
13     cout << endl;      

c.push_back(num)      在末尾增加一個元素。

c.pop_back()      删除末尾的元素。

c.push_front(num)      在開始位置增加一個元素。

c.pop_front()      删除第一個元素。

1     list<int> a1{1,2,3,4,5};
 2     a1.push_back(10);
 3     list<int>::iterator it;
 4     cout << "push_back:";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a1.pop_back();
11     cout << "pop_back:";
12     for(it = a1.begin();it!=a1.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;
16     
17     a1.push_front(20);
18     cout << "push_front:";
19     for(it = a1.begin();it!=a1.end();it++){
20         cout << *it << " ";
21     }
22     cout << endl;
23     
24     a1.pop_front();
25     cout << "pop_front:";
26     for(it = a1.begin();it!=a1.end();it++){
27         cout << *it << " ";
28     }
29     cout << endl;      

resize(n)      從新定義連結清單的長度,超出原始長度部分用0代替,小于原始部分删除。

resize(n,num)            從新定義連結清單的長度,超出原始長度部分用num代替。

1     list<int> a1{1,2,3,4,5};
 2     a1.resize(8);
 3     list<int>::iterator it;
 4     cout << "resize(n):";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a1.resize(10, 10);
11     cout << "resize(n,num):";
12     for(it = a1.begin();it!=a1.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;      

c1.swap(c2);      将c1和c2交換。

swap(c1,c2);      同上。

1     list<int> a1{1,2,3,4,5},a2,a3;
 2     a2.swap(a1);
 3     list<int>::iterator it;
 4     cout << "a2.swap(a1):";
 5     for(it = a2.begin();it!=a2.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     swap(a3,a2);
11     cout << "swap(a3,a2):";
12     for(it = a3.begin();it!=a3.end();it++){
13         cout << *it << " ";
14     }
15     return 0;      

c1.merge(c2)      合并2個有序的連結清單并使之有序,從新放到c1裡,釋放c2。

c1.merge(c2,comp)      合并2個有序的連結清單并使之按照自定義規則排序之後從新放到c1中,釋放c2。

1     list<int> a1{1,2,3},a2{4,5,6};
 2     a1.merge(a2);
 3     list<int>::iterator it;
 4     cout << "a1.merge(a2):";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a2.merge(a1,[](int n1,int n2){return n1>n2;});
11     cout << "a2.merge(a1,comp):";
12     for(it = a2.begin();it!=a2.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;      

c1.splice(c1.beg,c2)      将c2連接配接在c1的beg位置,釋放c2

1     list<int> a1{1,2,3},a2{4,5,6};
2     a1.splice(a1.begin(), a2);
3     list<int>::iterator it;
4     cout << "a1.merge(a2):";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;      

c1.splice(c1.beg,c2,c2.beg)      将c2的beg位置的元素連接配接到c1的beg位置,并且在c2中施放掉beg位置的元素

1     list<int> a1{1,2,3},a2{4,5,6};
2     a1.splice(a1.begin(), a2,++a2.begin());
3     list<int>::iterator it;
4     cout << "a1.merge(a2):";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
9     return 0;      

c1.splice(c1.beg,c2,c2.beg,c2.end)      将c2的[beg,end)位置的元素連接配接到c1的beg位置并且釋放c2的[beg,end)位置的元素

1     list<int> a1{1,2,3},a2{4,5,6};
2     a1.splice(a1.begin(),a2,a2.begin(),a2.end());
3     list<int>::iterator it;
4     cout << "a1.merge(a2):";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
9     return 0;      

remove(num)             删除連結清單中比對num的元素。

1     list<int> a1{1,2,3,4,5};
2     a1.remove(3);
3     list<int>::iterator it;
4     cout << "remove():";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;      

remove_if(comp)       删除條件滿足的元素,參數為自定義的回調函數。

1     list<int> a1{1,2,3,4,5};
2     a1.remove_if([](int n){return n<3;});
3     list<int>::iterator it;
4     cout << "remove_if():";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;      

reverse()       反轉連結清單

1     list<int> a1{1,2,3,4,5};
2     a1.reverse();
3     list<int>::iterator it;
4     cout << "reverse:";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;      

unique()       删除相鄰的元素

1     list<int> a1{1,1,3,3,5};
2     a1.unique();
3     list<int>::iterator it;
4     cout << "unique:";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
9     return 0;      

c.sort()       将連結清單排序,預設升序

c.sort(comp)       自定義回調函數實作自定義排序

1     list<int> a1{1,3,2,5,4};
 2     a1.sort();
 3     list<int>::iterator it;
 4     cout << "sort():";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a1.sort([](int n1,int n2){return n1>n2;});
11     cout << "sort(function point):";
12     for(it = a1.begin();it!=a1.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;      

重載運算符

operator==

operator!=

operator<

operator<=

operator>

operator>=

轉載請注明本文位址: STL之list容器詳解