list 容器
基本概念
- 功能:将資料進行鍊式存儲
- 連結清單(list)是一種實體存儲單元上非連續的存儲結構,資料元素的邏輯順序是通過連結清單中的指針連結實作的
- 連結清單由結點組成
- 結點由資料域和指針域組成
- list的底層是一個雙向循環連結清單
- list中的疊代器隻支援前移和後移,屬于雙向疊代器
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbwxCdh1mcvZ2LcV2Zh1Wa9M3clN2byBXLzN3btg3Pj1mY3VzUZdXSU1EMF1mT4NGROdXUUxkaKd1TpFzQOBTRE5EdZdlTpJ1VMtGbE5UMJdkT0kFVMlXSE10MZRlT1cGVPNzaU5Ee4MUT5lkeNVTWU1kdj1mY3lzQNlXQq1kdBpHTsZFWjFDbzwEd5ITW1NXbjhGetJWd0ckWqlTeMZTTINGMShUYvwlbj5yZtlmbkN3YuQnclZnbvN2Ztl2Lc9CX6MHc0RHaiojIsJye.jpg)
- list的優點:
- 采用動态記憶體配置設定,不會産生空間浪費或溢出
- 連結清單執行插入和删除操作友善,無需移動大量元素
- list的缺點:
- 連結清單有一個額外的指針域耗費空間,周遊時耗費時間
- 插入和删除操作不會導緻原疊代器失效,這與vector不同
list構造函數
/*
構造函數
list<T> name;
list(beg,end);
list(n,elem);
list(const list& list);
*/
void test01()
{
list<int> L1;
list<int> L2(3, 10);
list<int> L3(L2);
list<int> L4(L3.begin(), L3.end());
printList(L1);
printList(L2);
printList(L3);
printList(L4);
}
list指派和交換
/*
list指派和交換
assign(beg,end);
assign(n,elem);
list& operator=(const list& name);
swap(list);
*/
void test02()
{
list<int> L1(3, 100);
list<int> L2(3, 99);
list<int> L3(5, 10);
cout << "L1:";
printList(L1); //100 100 100
cout << "L2:";
printList(L2); //99 99 99
L1.swap(L2);
cout << "交換後" << endl;
cout << "L1:";
printList(L1); //99 99 99
cout << "L2:";
printList(L2); //100 100 100
L1.assign(5,1);
cout << "L1:";
printList(L1); //1 1 1 1 1
L2 = L1;
cout << "L2:";
printList(L2); //1 1 1 1 1
L1.assign(L3.begin(), L3.end());
cout << "L1:";
printList(L1); //10 10 10 10 10
}
list大小操作
/*
list大小操作
size(); 傳回list容器中元素的個數
empty(); 判斷list容器是否為空
resize(n); 重新指定容器長度為n,若變長則填充 預設填充值 ,若變短則 删除 多餘元素
resize(n,elem); 重新指定容器長度為n,若變長則填充 elem ,變變短則 删除 多餘元素
*/
void test03()
{
list<int> L1;
L1.assign(2,5);
cout << "Size:" << L1.size() << endl; //2
if (!L1.empty())
cout << "不空" << endl; //不空
else
cout << "空" << endl;
L1.resize(5);
printList(L1); //5 5 0 0 0
L1.resize(7, 1);
printList(L1); //5 5 0 0 0 1 1
}
list插入和删除
/*
list插入和删除
push_back(elem); 尾插
push_front(elem); 首插
pop_back(); 尾删
pop_front(); 首删
insert(pos,elem); 在疊代器pos位置插入 elem 元素
insert(pos,n,elem); 在疊代器pos位置插入 n個elem 元素
insert(pos,beg,end); 在疊代器pos位置插入 [beg,end)區間的 元素
erase(beg,end); 删除[beg,end)區間的資料,傳回下一個資料的位置
erase(pos); 删除疊代器pos位置的資料,傳回下一個資料的位置
clear(); 清空容器中的資料
remove(elem); 删除所有與elem值比對的元素
*/
void test04()
{
list<int> L1,L2(3,0);
L1.push_back(1);
L1.push_back(1);
L1.push_front(2);
L1.push_front(2);
printList(L1); // 2 2 1 1
L1.pop_back();
L1.pop_front();
printList(L1); // 2 1
L1.insert(L1.begin(), 0);
printList(L1); //0 2 1
L1.insert(L1.begin(), 3, 1);
printList(L1); //1 1 1 0 2 1
L1.insert(L1.end(), L2.begin(), L2.end());
printList(L1); //1 1 1 0 2 1 0 0 0
L2.erase(L2.begin(), --L2.end());
printList(L2); //0
L1.erase(--L1.end());
printList(L1); //1 1 1 0 2 1 0 0
L2.clear();
cout << "L2 Size:" << L2.size()<<endl; //0
L1.remove(1);
printList(L1); //0 2 0 0
}
list資料存取
/*
list資料存取
front(); 擷取鍊頭資料
back(); 擷取鍊尾資料
*/
void test05()
{
list<int> L1;
L1.push_back(1);
L1.push_back(2);
L1.push_back(3);
int front = L1.front();
int back = L1.back();
cout << "front:" << front << endl; // 1
cout << "back:" << back << endl; // 3
}
總結:
list容器由于底層資料結構是連結清單,是以無法通過[]和at來通路資料
list反轉和排序
/*
list反轉和排序
reverse(); 反轉連結清單
sort(); 排序連結清單
*/
bool _sort(int x, int y)
{
return x > y;
}
void test06()
{
list<int> L1, L2;
L1.push_back(1);
L1.push_back(2);
L1.push_back(3);
L2.push_back(3);
L2.push_back(2);
L2.push_back(1);
printList(L1); //1 2 3
printList(L2); //3 2 1
L1.reverse();
printList(L1); //3 2 1
L2.sort();
printList(L2); //1 2 3
L2.sort(_sort);
printList(L2); //3 2 1
}
push_back(2);
L2.push_back(1);
printList(L1); //1 2 3
printList(L2); //3 2 1
L1.reverse();
printList(L1); //3 2 1
L2.sort();
printList(L2); //1 2 3
L2.sort(_sort);
printList(L2); //3 2 1
}