C++入門之list的使用詳解
前言
今天我們終于來到了C++的list章節(jié),在講解之前,先回顧一下前面的vector和string吧.
vector和string的底層都是用的順序表,因此其空間在物理結構上連續(xù)的.而今天的list卻不一樣,它在物理上是散亂的.因為list本質上是一個鏈表!,并且是一個帶頭雙向循環(huán)鏈表,在前面的數據結構章節(jié),還記得博主的鏈表實現嗎?還疑惑博主為什么對于鏈表的起名很怪嗎?因為就是為了今天的list講解呀~
今天博主也主要將從list的構造使用,迭代器使用,相關容量操作,以及元素訪問和數據修改等方面進行闡述
構造的使用
構造函數的使用主要有4個,分別如下
| list() | 構造空的list |
|---|---|
| list (size_type n, const value_type& val = value_type()) | 構造的list中包含n個值為val的元素 |
| list (const list& x) | 拷貝構造函數 |
| list (InputIterator first, InputIterator last) | 用[first, last)區(qū)間中的元素構造list |
1 構造空list
不需要傳入任何參數,直接利用list類模板定義對象
list<int> l1; //定義int型鏈表 list<char> l2; //定義char型鏈表 list<double> l3; //定義double型鏈表 //上面的三個對象,內容都空
2 構造含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 拷貝構造
即傳入一個同類型的list
list<int> l1(4,5); //定義int型鏈表,含有4個5 list<int> l2(l1); //把l1的內容復制一份給了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的內容是 'w' 'w' 'w'
list<int> l3(num,num + 3); //因為num+3的位置,索引為3,但是迭代區(qū)間左閉右開,所以不包括索引3位置,內容為1 2 3
迭代器接口
C++提供了如下:
| 函數聲明 | 接口說明 |
|---|---|
| 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)建內容為1 2 3 4 5的鏈表
list<int>::iterator it = li.begin();
while(it = li.end())
{
cout<<*it<<" ";
it++;
}
//輸出結果為: 1 2 3 4 5
2 逆向迭代接口
int num[5] = {1,2,3,4,5};
list<int> li(num,num+5); //創(chuàng)建內容為1 2 3 4 5的鏈表
list<int>::iterator it = li.rbegin();
while(it = li.rend())
{
cout<<*it<<" ";
it++;
}
//輸出結果為: 5 4 3 2 1
容量接口
主要有兩個,如下:
| 函數聲明 | 接口說明 |
|---|---|
| empty() | 檢測list是否為空,是返回true,否則返回false |
| size() | 返回list中有效節(jié)點的個數 |
int num[5] = {1,2,3,4,5};
list<int> li(num,num+5); //創(chuàng)建內容為1 2 3 4 5的鏈表
list<int> li1;
if(li.empty())
{
cout<<"list沒有數據"<<endl;
}
else
{
cout<<"list有"<<li.size()<<"個元素"<<endl;
}
if(li1.empty())
{
cout<<"list1沒有數據"<<endl;
}
else
{
cout<<"list1有"<<li1.size()<<"個元素"<<endl;
}
/* 輸出結果為:
list有5個元素
list1沒有數據
*/
元素訪問
這里c++提供了兩個接口,分別用于首尾訪問front() 和 back();
int num[5] = {1,2,3,4,5};
list<int> li(num,num+5); //創(chuàng)建內容為1 2 3 4 5的鏈表
cout << "front獲取的元素為:"<<li.front()<<endl;
cout << "back獲取的元素為:"<<li.back()<<endl;
/* 結果為:
front獲取的元素為: 1
back獲取的元素為: 5
*/
數據修改
這里主要提供了如下接口:
| 函數聲明 | 接口說明 |
|---|---|
| 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); //現在list的內容為:9 2 3
頭刪
list<char> li(3,'s'); li.pop_front(); //現在list的內容為:s s
尾插
list<char> li(3,'s');
li.push_back('a');
//現在list的內容為:s s s a
尾刪
list<int> li(4,2); li.pop_back(); //現在的list內容為: 2 2 2
pos位置插入
這里博主先介紹一個全局函數find(),它是一個函數模板
template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);
即我們需要傳三個參數,前兩個是迭代器區(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); //現在的list內容為: 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); //現在的list內容為: 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); //交換鏈表
//現在li1為: 5 4 3 2 1
//現在li2為: 1 2 3 4
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
詳解VS2019+OpenCV-4-1-0+OpenCV-contrib-4-1-0
這篇文章主要介紹了詳解VS2019+OpenCV-4-1-0+OpenCV-contrib-4-1-0,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04

