- list是可以在常數範圍內在任意位置進行插入和刪除的序列式容器,並且該容器可以前後雙向迭代。
- list的底層是雙向鏈表結構,雙向鏈表中每個元素存儲在互不關聯的獨立節點中,在節點中通過指針指向其前一個元素和後一個元素。
- list與forward_list非常相似:最主要的不同在於forward_list是單鏈表,只能朝前迭代,已讓其更簡單高效。
- 與其他序列式容器相比(array,vector,deque),list通常在任意位置進行插入,移除元素的執行效率更好。
- 與其他序列容器相比,list和forward_list最大缺陷是不支持任意位置的隨機訪問,比如:要訪問list的第六個元素,必須從已知的位置(比如頭部或者尾部)迭代到該位置,在這段位置上迭代需要線性的時間開銷:list還需要一些額外的空間,以保存每個節點的相關聯信息
- list的構造
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int> l1;//構造空的l1
list<int> l2(4, 100);//l2中放4個值爲100的元素
list<int> l3(l2.begin(),l2.end());//用l2的begin(),end()左閉右開的區間構造l3
list<int> l4(l3);
//以數組爲迭代器區間構造l5
int arr[] = {16,2,77,29};
list<int> l5(arr,arr+sizeof(arr)/sizeof(int));
//用迭代器方式來打印l5中的元素
for (list<int>::iterator it = l5.begin(); it != l5.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//c++11迭代器打印
for (auto& x : l5)
{
cout << x << " ";
}
cout << endl;
return 0;
}
- list iterator的使用
#include<iostream>
#include<list>
using namespace std;
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,0};
//使用數組作爲迭代器進行構造
list<int> l(arr, arr +sizeof(arr)/sizeof(int) );
//使用正向迭代器正向打印list中的元素
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//使用反向迭代器逆向打印list中的元素
for (list<int>::reverse_iterator it = l.rbegin(); it != l.rend();it++)
{
cout << *it << " ";
}
cout << endl;
//const的正向迭代器
auto cit = l.cbegin();
cout << typeid(cit).name() << endl;
for (auto &xx : l)
{
cout << xx << " ";
}
cout << endl;
return 0;
}
此處先將迭代器理解成一個指針,該指針指向list中的某個節點
- begin與end爲正向迭代器,對迭代器執行++操作,迭代器向前移動
- rbegin(end)與rend(begin)爲反向迭代器,對迭代器執行++操作,迭代器向前移動
- cbegin與cend爲const的正向迭代器,與begin和end不同的是:該迭代器指向的節點的值不能被修改
- crbegin與crend爲const的反向迭代器,與rbegin和rend不同的是:該迭代器指向節點的元素值不能被修改
- list capacity
#include<iostream>
#include<list>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
//用數組當作迭代器進行構造
list<int> l(arr, arr + sizeof(arr) / sizeof(int));
//打印list中有效節點的個數
cout << l.size() << endl;
//檢查list是否爲空
if (l.empty())
{
cout << "空的list" << endl;
}
else
{
for (auto &e : l)
{
cout << e << " ";
}
cout << endl;
}
return 0;
}
- list element access
#include<iostream>
#include<list>
using namespace std;
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,0};
list<int> l1(arr, arr + sizeof(arr) / sizeof(int));
for (auto& e : l1)
{
cout << e << " ";
}
cout << endl;
//將list中的第一個節點與最後一個節點中的值改爲10
l1.front() = 10; //list的第一個節點
l1.back() = 10;//list的第二個節點
for (auto& e : l1)
{
cout << e << " ";
}
cout << endl;
const list<int> l2(arr, arr + sizeof(arr) / sizeof(arr[0]));
const int& ca = l2.front();
return 0;
}
- list modifiers
#include<iostream>
#include<list>
using namespace std;
void PrintList(list<int>& l)
{
for (auto &x : l)
{
cout << x << " ";
}
cout << endl;
}
void TestList1()
{
int arr[] = { 1, 2, 3 };
list<int> L(arr, arr + sizeof(arr) / sizeof(arr[0]));
//在list尾部插入4,頭部插入0
L.push_back(4);
L.push_front(0);
PrintList(L);
//刪除list尾部節點和頭部節點
L.pop_back();
L.pop_front();
PrintList(L);
}
class Date{
public:
Date(int year=1900,int month=1,int day=1)
:_year(year)
, _month(month)
, _day(_day)
{
cout << "Date(int,int,int)" << this << endl;
}
Date(const Date& d)
:_year(d._year)
, _month(d._month)
, _day(d._day)
{
cout << "Date(const Date&)" << this << endl;
}
private:
int _year;
int _month;
int _day;
};
//push_back尾插:先構造好元素,然後將元素拷貝到節點中,插入時先調構造函數,在調拷貝構造函數
//emplace_back尾插:先構造節點,然後調用構造節點,然後調用構造函數在節點中直接構造對象
//emplace_back比push_back更高效,少一次拷貝構造函數的調用
void TestList2()
{
list<Date> l;
Date d(2018, 10, 21);
l.emplace_back(2018, 10, 21);
l.emplace_front(2018, 10, 19);
}
void TestList3()
{
int arr[] = {1,2,3};
list<int> L(arr, arr + sizeof(arr) / sizeof(arr[0]));
//獲取鏈表中第二個節點
auto pos = ++L.begin();
cout << *pos << endl;
//在pos前插入值爲4的元素
L.insert(pos, 4);
PrintList(L);
//在pos前插入v.begin(),v.end()區間中的元素
vector<int> v{7,8,9};
L.insert(pos, v.begin(), v.end());
PrintList(L);
//刪除pos位置上的元素
L.erase(pos);
PrintList(L);
//刪除list中begin,end區間中的元素,即刪除list中的所以元素
L.erase(L.begin(), L.end());
PrintList(L);
}
void TestList4()
{
//用數組來構造list
int arr[] = { 1, 2, 3 };
list<int> l1(arr,arr+sizeof(arr)/sizeof(arr[0]));
PrintList(l1);
//將l1中元素個數增加到10個,多出元素用默認值填充
l1.resize(10);
PrintList(l1);
//將l1中的元素增加到20個,多出的元素用4來填充
l1.resize(20, 4);
PrintList(l1);
//將l1中的元素減少到5個
l1.resize(5);
PrintList(l1);
//用vector中的元素來構造list
vector<int> v{ 4, 5, 6 };
list<int> l2(v.begin(), v.end());
PrintList(l2);
//交換l1和l2中的元素
l1.swap(l2);
PrintList(l1);
PrintList(l2);
//將l2中的元素清空
l2.clear();
cout << l2.size() << endl;
}
int main(){
//TestList2();
TestList4();
return 0;
}
- list的迭代器失效
迭代器失效即迭代器所指向的節點的無效,即該節點被刪除了。因爲list的底層結構爲帶頭節點的雙向循環鏈表,因此在list中進程插入時是不會導致list的迭代器失效的,只有在刪除時纔會失效,並且失效的只是指向被刪除的節點的迭代器,其他迭代器不會受到影響。