日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

STL 之 list 容器详解

發布時間:2025/5/22 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 容器 好文要頂 關注我 收藏該文 Sam大叔
關注 - 0
粉絲 - 11 +加關注 4 0 ? 上一篇:STL之vector容器詳解
? 下一篇:STL之deque容器詳解
posted on 2013-01-08 16:13 Sam大叔 閱讀(23245) 評論(3) 編輯 收藏

總結

以上是生活随笔為你收集整理的STL 之 list 容器详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。