欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++入門之list的使用詳解

 更新時間:2021年11月18日 14:54:06   作者:捕獲一只小肚皮  
這篇文章主要為大家介紹了C++入門之list的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

前言

今天我們終于來到了C++的list章節(jié),在講解之前,先回顧一下前面的vector和string吧.

vector和string的底層都是用的順序表,因此其空間在物理結(jié)構(gòu)上連續(xù)的.而今天的list卻不一樣,它在物理上是散亂的.因為list本質(zhì)上是一個鏈表!,并且是一個帶頭雙向循環(huán)鏈表,在前面的數(shù)據(jù)結(jié)構(gòu)章節(jié),還記得博主的鏈表實現(xiàn)嗎?還疑惑博主為什么對于鏈表的起名很怪嗎?因為就是為了今天的list講解呀~

今天博主也主要將從list的構(gòu)造使用,迭代器使用,相關(guān)容量操作,以及元素訪問和數(shù)據(jù)修改等方面進行闡述

構(gòu)造的使用

構(gòu)造函數(shù)的使用主要有4個,分別如下

list() 構(gòu)造空的list
list (size_type n, const value_type& val = value_type()) 構(gòu)造的list中包含n個值為val的元素
list (const list& x) 拷貝構(gòu)造函數(shù)
list (InputIterator first, InputIterator last) 用[first, last)區(qū)間中的元素構(gòu)造list

1 構(gòu)造空list

不需要傳入任何參數(shù),直接利用list類模板定義對象

list<int> l1;        //定義int型鏈表 
list<char> l2;       //定義char型鏈表 
list<double> l3;     //定義double型鏈表 
//上面的三個對象,內(nèi)容都空

2 構(gòu)造含n個值為val的元素

按照上面的定義直接傳參即可

list<int> l1(4,5);           //定義int型鏈表,含有4個5 
list<char> l2(3,'s');        //定義char型鏈表,含有3個's' 
list<double> l3(4,2.3);     //定義double型鏈表,含有4個2.3 

3 拷貝構(gòu)造

即傳入一個同類型的list

list<int> l1(4,5);           //定義int型鏈表,含有4個5 
list<int> l2(l1);            //把l1的內(nèi)容復制一份給了l2

4 用迭代區(qū)間

**這里有個注意點,迭代區(qū)間是左閉右開的!**即不包含右邊界.

int num[4] = {1,2,3,4};
list<char> l1(3,'w');
list<char> l2(l1.begin(),l1.end());  //end()是最后一個元素位置的下一個元素位置,所以不包括,因此l2的內(nèi)容是 'w' 'w' 'w'
list<int> l3(num,num + 3);       //因為num+3的位置,索引為3,但是迭代區(qū)間左閉右開,所以不包括索引3位置,內(nèi)容為1 2 3

迭代器接口

C++提供了如下:

函數(shù)聲明 接口說明
begin() + end() 返回第一個元素的迭代器+返回最后一個元素下一個位置的迭代器
rbegin() + rend() 返回第一個元素的reverse_iterator,即end位置 + 返回最后一個元素下一個位置的reverse_iterator,即begin位置

1 正常迭代接口

int num[5] = {1,2,3,4,5};
list<int> li(num,num+5);    //創(chuàng)建內(nèi)容為1 2 3 4 5的鏈表
list<int>::iterator it = li.begin();
while(it = li.end())
{
    cout<<*it<<" ";
    it++;
}
//輸出結(jié)果為: 1 2 3 4 5

2 逆向迭代接口

int num[5] = {1,2,3,4,5};
list<int> li(num,num+5);    //創(chuàng)建內(nèi)容為1 2 3 4 5的鏈表
list<int>::iterator it = li.rbegin();
while(it = li.rend())
{
    cout<<*it<<" ";
    it++;
}
//輸出結(jié)果為: 5 4 3 2 1

容量接口

主要有兩個,如下:

函數(shù)聲明 接口說明
empty() 檢測list是否為空,是返回true,否則返回false
size() 返回list中有效節(jié)點的個數(shù)
int num[5] = {1,2,3,4,5};
list<int> li(num,num+5);    //創(chuàng)建內(nèi)容為1 2 3 4 5的鏈表
list<int> li1;
if(li.empty())   
{
    cout<<"list沒有數(shù)據(jù)"<<endl;
}
else 
{
    cout<<"list有"<<li.size()<<"個元素"<<endl;
}
if(li1.empty())   
{
    cout<<"list1沒有數(shù)據(jù)"<<endl;
}
else 
{
    cout<<"list1有"<<li1.size()<<"個元素"<<endl;
}
/* 輸出結(jié)果為:  
list有5個元素
list1沒有數(shù)據(jù)
*/

元素訪問

這里c++提供了兩個接口,分別用于首尾訪問front() 和 back();

int num[5] = {1,2,3,4,5};
list<int> li(num,num+5);    //創(chuàng)建內(nèi)容為1 2 3 4 5的鏈表
cout << "front獲取的元素為:"<<li.front()<<endl;
cout << "back獲取的元素為:"<<li.back()<<endl;
/* 結(jié)果為:
front獲取的元素為: 1
back獲取的元素為:  5
*/

數(shù)據(jù)修改

這里主要提供了如下接口:

函數(shù)聲明 接口說明
push_front() 在list首元素前插入值為val的元素
pop_front() 刪除list中第一個元素
push_back() 在list尾部插入值為val的元素
pop_back() 刪除list中最后一個元素
insert(iterator pos,const value_type& val) 在list position 位置中插入值為val的元素
erase(iterator pos) 刪除list position位置的元素
swap() 交換兩個list中的元素

頭插

list<int> li(2,3);
li.push_front(9);
//現(xiàn)在list的內(nèi)容為:9 2 3 

頭刪

list<char> li(3,'s');
li.pop_front();
//現(xiàn)在list的內(nèi)容為:s s 

尾插

list<char> li(3,'s');
li.push_back('a');
//現(xiàn)在list的內(nèi)容為:s s s a

尾刪

list<int> li(4,2);
li.pop_back();
//現(xiàn)在的list內(nèi)容為: 2 2 2

pos位置插入

這里博主先介紹一個全局函數(shù)find(),它是一個函數(shù)模板

template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);

即我們需要傳三個參數(shù),前兩個是迭代器區(qū)間,后是待查找值,其中迭代器區(qū)間是左閉右開.

list<int> li;
li.push_bakc(1);
li.push_bakc(2);
li.push_bakc(3);
list<int>::iterator it = li.begin();
it = find(it,it+3,2)       //找到元素2的位置
li.insert(it,66);
//現(xiàn)在的list內(nèi)容為: 1 66 2 3

erase擦除pos位置

list<int> li;
li.push_bakc(1);
li.push_bakc(2);
li.push_bakc(3);
list<int>::iterator it = li.begin();
it = find(it,it+3,2)           //找到元素2的位置
li.erase(it);
//現(xiàn)在的list內(nèi)容為: 1 3    

交換兩個鏈表元素

int num1[4] = {1,2,3,4};
int num2[5] = {5,4,3,2,1};
list<int> li1(num1,num1 + 4);
list<int> li2(num2,num2 + 5);
li1.swap(li2); //交換鏈表
//現(xiàn)在li1為: 5 4 3 2 1
//現(xiàn)在li2為: 1 2 3 4

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

最新評論