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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

STL:list用法详解

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

list容器介紹

相對(duì)于vector容器的連續(xù)線性空間,list是一個(gè)雙向鏈表,它有一個(gè)重要性質(zhì):插入操作和刪除操作都不會(huì)造成原有的list迭代器失效,每次插入或刪除一個(gè)元素就配置或釋放一個(gè)元素空間。也就是說,對(duì)于任何位置的元素插入或刪除,list永遠(yuǎn)是常數(shù)時(shí)間。

常用函數(shù)

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

list<Elem> c:創(chuàng)建一個(gè)空的list

list<Elem> c1(c2):復(fù)制另一個(gè)同類型元素的list

list<Elem>c(n):創(chuàng)建n個(gè)元素的list,每個(gè)元素值由默認(rèn)構(gòu)造函數(shù)確定

list<Elem>c(n,elem):創(chuàng)建n個(gè)元素的list,每個(gè)元素的值為elem

list<Elem>c(begin,end):由迭代器創(chuàng)建list,迭代區(qū)間為[begin,end)

(2)????大小、判斷函數(shù)

Int size() const:返回容器元素個(gè)數(shù)

bool empty() const:判斷容器是否為空,若為空則返回true

(3)????增加、刪除函數(shù)

void push_back(const T& x):list元素尾部增加一個(gè)元素x

void push_front(const T& x):list元素首元素錢添加一個(gè)元素X

void pop_back():刪除容器尾元素,當(dāng)且僅當(dāng)容器不為空

void pop_front():刪除容器首元素,當(dāng)且僅當(dāng)容器不為空

void remove(const T& x):刪除容器中所有元素值等于x的元素

void clear():刪除容器中的所有元素

iterator insert(iterator it, const T& x ):在迭代器指針it前插入元素x,返回x迭代器指針

void insert(iterator it,size_type n,const T& x):迭代器指針it前插入n個(gè)相同元素x

void insert(iterator it,const_iterator first,const_iteratorlast):把[first,last)間的元素插入迭代器指針it前

iterator erase(iterator it):刪除迭代器指針it對(duì)應(yīng)的元素

iterator erase(iterator first,iterator last):刪除迭代器指針[first,last)間的元素

(4)????遍歷函數(shù)

iterator begin():返回首元素的迭代器指針

iterator end():返回尾元素之后位置的迭代器指針

reverse_iterator rbegin():返回尾元素的逆向迭代器指針,用于逆向遍歷容器

reverse_iterator rend():返回首元素前一個(gè)位置的迭代器指針

reference front():返回首元素的引用

reference back():返回尾元素的引用?

(5)????操作函數(shù)

void sort():容器內(nèi)所有元素排序,默認(rèn)是升序

template<class Pred>void sort(Pred pr):容器內(nèi)所有元素根據(jù)預(yù)斷定函數(shù)pr排序

void swap(list& str):兩list容器交換功能

void unique():容器內(nèi)相鄰元素若有重復(fù)的,則僅保留一個(gè)

void splice(iterator it,list& li):隊(duì)列合并函數(shù),隊(duì)列l(wèi)i所有函數(shù)插入迭代指針it前,x變成空隊(duì)列

void splice(iterator it,list& li,iterator first):隊(duì)列l(wèi)i中移走[first,end)間元素插入迭代指針it前

void splice(iterator it,list& li,iterator first,iterator last):x中移走[first,last)間元素插入迭代器指針it前

void reverse():反轉(zhuǎn)容器中元素順序

基本操作示例:

#include "stdafx.h"
#include<iostream>
#include<string>
#include<list>
using namespace std;
typedef list<string> LISTSTR;


int _tmain(int argc, _TCHAR* argv[])
{
LISTSTR test;
test.push_back("back");
test.push_back("middle");
test.push_back("front");


cout<<test.front()<<endl;
cout<<*test.begin()<<endl;


cout<<test.back()<<endl;
cout<<*(test.rbegin())<<endl;


test.pop_front();
test.pop_back();


cout<<test.front()<<endl;
return 0;
}


程序運(yùn)行結(jié)果如下:
從上述代碼可以看出list首尾元素的增加和刪除都是非常容易的,test.front()相當(dāng)于string& s=test.front(),返回了首元素的引用;test.begin()相當(dāng)于list<string>::iterator it=test.begin(),返回了首元素的迭代器指針,因此test.front()于*test.begin()的結(jié)果是一致的。 [cpp]?view plaincopy
  • #include?"stdafx.h"??
  • #include<iostream>??
  • #include<string>??
  • #include<list>??
  • using?namespace?std;??
  • typedef?list<int>?LISTINT;??
  • ??
  • int?_tmain(int?argc,?_TCHAR*?argv[])??
  • {??
  • ????LISTINT?test;??
  • ????for(int?i=0;i<5;i++)??
  • ????{??
  • ????????test.push_back(i+1);??
  • ????}??
  • ??
  • ????LISTINT::iterator?it?=?test.begin();??
  • ????for(;it!=test.end();it++)??
  • ????{??
  • ????????cout<<*it<<"\t";??
  • ????}??
  • ????cout<<endl;??
  • ??
  • ????//reverse?show??
  • ????LISTINT::reverse_iterator?rit?=?test.rbegin();??
  • ????for(;rit!=test.rend();rit++)??
  • ????{??
  • ????????cout<<*rit<<"\t";??
  • ????}??
  • ????cout<<endl;??
  • ????return?0;??
  • }??

  • 程序運(yùn)行結(jié)果如下:
    正向迭代器與逆向迭代器表示形式是不一樣的,前者是iterator,后者是reverse_iterator。逆向顯示并不會(huì)改變?cè)卦谌萜髦械奈恢?#xff0c;只是逆向顯示。 [cpp]?view plaincopy
  • #include?"stdafx.h"??
  • #include<iostream>??
  • #include<string>??
  • #include<list>??
  • using?namespace?std;??
  • typedef?list<int>?LISTINT;??
  • ??
  • int?_tmain(int?argc,?_TCHAR*?argv[])??
  • {??
  • ????LISTINT?test;??
  • ????test.push_back(1);??
  • ????test.push_back(5);??
  • ????test.push_back(3);??
  • ????test.push_back(10);??
  • ??
  • ????LISTINT?test2;??
  • ????test2.push_back(2);??
  • ????test2.push_back(8);??
  • ????test2.push_back(6);??
  • ????test2.push_back(9);??
  • ??
  • ????test.sort();??
  • ????test2.sort();??
  • ??
  • ????test.merge(test2);??
  • ??
  • ????for(LISTINT::iterator?it?=?test.begin();it!=test.end();it++)??
  • ????{??
  • ????????cout<<*it<<"\t";??
  • ????}??
  • ????cout<<endl;??
  • ????cout<<test.size()<<"\t"<<test2.size()<<endl;??
  • ????return?0;??
  • }??

  • 上面的代碼展示了sort merge和splice的使用,程序運(yùn)行結(jié)果如下:
    從允許結(jié)果可以看出,兩個(gè)鏈表merge合并前,一般都已經(jīng)俺升序排好序,合并后的鏈表仍然是升序排列。merge操作是數(shù)據(jù)移動(dòng)操作,不是復(fù)制操作,因此t1.merge(t2)表示把test2中所有元素依次移動(dòng)并插入到源鏈表test的適當(dāng)位置,test增加了多少個(gè)元素,test2就減少了多少個(gè)元素。若用test.splice(test.begin(),test2)代替程序中的test.merge(test2),其余不變,就能看出splice的特點(diǎn)。splice()完成的是拼接功能,也是數(shù)據(jù)移動(dòng)操作,不慎復(fù)制操作。test.splice(test.begin(),test2)表明把test2中所有元素整體地移動(dòng)到原始鏈表test的首元素前,test增加了多少個(gè)元素,test2就減少了多少個(gè)元素。如上述代碼所述,test,test2排序后,test={1,3,5,10},test2={2,6,8,9}. test.splice(test.begin(),test2)后,test={2,6,8,9,1,3,5,10},test2={};test.merge(test2)后,test={1,2,3,5,6,8,9,10},test2={}

    總結(jié)

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

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