日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

STL 之 list 容器详解

發(fā)布時(shí)間:2025/5/22 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL 之 list 容器详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
STL之list容器詳解

List 容器

list是C++標(biāo)準(zhǔn)模版庫(kù)(STL,Standard Template Library)中的部分內(nèi)容。實(shí)際上,list容器就是一個(gè)雙向鏈表,可以高效地進(jìn)行插入刪除元素。

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

list屬于std命名域的內(nèi)容,因此需要通過命名限定:using std::list;也可以直接使用全局的命名空間方式:using namespace std;

構(gòu)造函數(shù)

?  ?list<int> c0; //空鏈表

  list<int> c1(3); //建一個(gè)含三個(gè)默認(rèn)值是0的元素的鏈表

  list<int> c2(5,2); //建一個(gè)含五個(gè)元素的鏈表,值都是2

  list<int> c4(c2); //建一個(gè)c2的copy鏈表

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

成員函數(shù)

c.begin()? ? ? 返回指向鏈表第一個(gè)元素的迭代器。

c.end()? ? ? 返回指向鏈表最后一個(gè)元素之后的迭代器。

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()? ? ? 返回逆向鏈表的第一個(gè)元素,即c鏈表的最后一個(gè)數(shù)據(jù)。

c.rend()? ? ? 返回逆向鏈表的最后一個(gè)元素的下一個(gè)位置,即c鏈表的第一個(gè)數(shù)據(jù)再往前的位置。

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=? ? ? 重載賦值運(yùn)算符。

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個(gè)num拷貝賦值給鏈表c。

c.assign(beg,end)? ? ? 將[beg,end)區(qū)間的元素拷貝賦值給鏈表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的第一個(gè)元素。

c.back()? ? ? 返回鏈表c的最后一個(gè)元素。

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中實(shí)際元素的個(gè)數(shù)。

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

c.max_size()? ? ? 返回鏈表c可能容納的最大元素?cái)?shù)量。

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個(gè)元素num。

c.insert(pos,beg,end)? ? ? 在pos位置插入?yún)^(qū)間為[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)? ? ? 在末尾增加一個(gè)元素。

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

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

c.pop_front()? ? ? 刪除第一個(gè)元素。

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)? ? ? 從新定義鏈表的長(zhǎng)度,超出原始長(zhǎng)度部分用0代替,小于原始部分刪除。

resize(n,num)? ? ? ? ? ? 從新定義鏈表的長(zhǎng)度,超出原始長(zhǎng)度部分用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個(gè)有序的鏈表并使之有序,從新放到c1里,釋放c2。

c1.merge(c2,comp)? ? ? 合并2個(gè)有序的鏈表并使之按照自定義規(guī)則排序之后從新放到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) ? ? ? 刪除條件滿足的元素,參數(shù)為自定義的回調(diào)函數(shù)。

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() ? ? ? 反轉(zhuǎn)鏈表

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() ? ? ? 將鏈表排序,默認(rèn)升序

c.sort(comp) ? ? ? 自定義回調(diào)函數(shù)實(shí)現(xiàn)自定義排序

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;

重載運(yùn)算符

operator==

operator!=

operator<

operator<=

operator>

operator>=

分類: STL 容器 好文要頂 關(guān)注我 收藏該文 Sam大叔
關(guān)注 - 0
粉絲 - 11 +加關(guān)注 4 0 ? 上一篇:STL之vector容器詳解
? 下一篇:STL之deque容器詳解
posted on 2013-01-08 16:13 Sam大叔 閱讀(23245) 評(píng)論(3) 編輯 收藏

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。