本篇內容主要講解“怎么使用C++中的list”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么使用C++中的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)區間中的元素構造list |
不需要傳入任何參數,直接利用list類模板定義對象
list<int> l1; //定義int型鏈表 list<char> l2; //定義char型鏈表 list<double> l3; //定義double型鏈表 //上面的三個對象,內容都空
按照上面的定義直接傳參即可
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
即傳入一個同類型的list
list<int> l1(4,5); //定義int型鏈表,含有4個5 list<int> l2(l1); //把l1的內容復制一份給了l2
**這里有個注意點,迭代區間是左閉右開的!**即不包含右邊界.
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,但是迭代區間左閉右開,所以不包括索引3位置,內容為1 2 3C++提供了如下:
| 函數聲明 | 接口說明 |
|---|---|
| begin() + end() | 返回第一個元素的迭代器+返回最后一個元素下一個位置的迭代器 |
| rbegin() + rend() | 返回第一個元素的reverse_iterator,即end位置 + 返回最后一個元素下一個位置的reverse_iterator,即begin位置 |
int num[5] = {1,2,3,4,5};
list<int> li(num,num+5); //創建內容為1 2 3 4 5的鏈表
list<int>::iterator it = li.begin();
while(it = li.end())
{
cout<<*it<<" ";
it++;
}
//輸出結果為: 1 2 3 4 5int num[5] = {1,2,3,4,5};
list<int> li(num,num+5); //創建內容為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中有效節點的個數 |
int num[5] = {1,2,3,4,5};
list<int> li(num,num+5); //創建內容為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); //創建內容為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 alist<int> li(4,2); li.pop_back(); //現在的list內容為: 2 2 2
這里博主先介紹一個全局函數find(),它是一個函數模板
template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);
即我們需要傳三個參數,前兩個是迭代器區間,后是待查找值,其中迭代器區間是左閉右開.
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
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到此,相信大家對“怎么使用C++中的list”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。