STL之list函數解析

STL之list函數解析

在這裏插入圖片描述
list是C++標準模版庫(STL,Standard Template Library)中的部分內容。實際上,list容器就是一個雙向鏈表,可以高效地進行插入刪除元素。

使用list容器之前必須加上頭文件:#include;

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;

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) 自定義回調函數實現自定義排序

list<int> a1{1,3,2,5,4};
    a1.sort();
    list<int>::iterator it;
    cout << "sort():";
    for(it = a1.begin();it!=a1.end();it++){
        cout << *it << " ";
    }
    cout << endl;
    
    a1.sort([](int n1,int n2){return n1>n2;});
    cout << "sort(function point):";
    for(it = a1.begin();it!=a1.end();it++){
        cout << *it << " ";
    }
    cout << endl;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章