一、概述
list 由雙向鏈表(doubly linked list)實現而成,元素也存放在堆中,每個元素都是放在一塊內存中,他的內存空間可以是不連續的,通過指針來進行數據的訪問,這個特點使得它的隨機存取變得非常沒有效率,因此它沒有提供 [] 操作符的重載。但是由於鏈表的特點,它可以很有效率的支持任意地方的插入和刪除操作。
二、定義及初始化
使用之前必須加相應容器的頭文件:
#include <list> // list屬於std命名域的,因此需要通過命名限定,例如using std::list;
定義的代碼如下:
list<int> a; // 定義一個int類型的列表a
list<int> a(10); // 定義一個int類型的列表a,並設置初始大小為10
list<int> a(10, 1); // 定義一個int類型的列表a,並設置初始大小為10且初始值都為1
list<int> b(a); // 定義並用列表a初始化列表b
deque<int> b(a.begin(), ++a.end()); // 將列表a中的第1個元素作為列表b的初始值
除此之外,還可以直接使用數組來初始化向量:
int n[] = { 1, 2, 3, 4, 5 };
list<int> a(n, n + 5); // 將數組n的前5個元素作為列表a的初值
三、基本操作
3.1 容量函數
- 容器大小:
lst.size();
- 容器最大容量:
lst.max_size();
- 更改容器大小:
lst.resize();
- 容器判空:
lst.empty();
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
list<int> lst;
for (int i = 0; i<6; i++)
{
lst.push_back(i);
}
cout << lst.size() << endl; // 輸出:6
cout << lst.max_size() << endl; // 輸出:357913941
lst.resize(0); // 更改元素大小
cout << lst.size() << endl; // 輸出:0
if (lst.empty())
cout << "元素為空" << endl; // 輸出:元素為空
return 0;
}
3.2 添加函數
- 頭部添加元素:
lst.push_front(const T& x);
- 末尾添加元素:
lst.push_back(const T& x);
- 任意位置插入一個元素:
lst.insert(iterator it, const T& x);
- 任意位置插入 n 個相同元素:
lst.insert(iterator it, int n, const T& x);
- 插入另一個向量的 [forst,last] 間的數據:
lst.insert(iterator it, iterator first, iterator last);
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
list<int> lst;
// 頭部增加元素
lst.push_front(4);
// 末尾添加元素
lst.push_back(5);
// 任意位置插入一個元素
list<int>::iterator it = lst.begin();
lst.insert(it, 2);
// 任意位置插入n個相同元素
lst.insert(lst.begin(), 3, 9);
// 插入另一個向量的[forst,last]間的數據
list<int> lst2(5, 8);
lst.insert(lst.begin(), lst2.begin(), ++lst2.begin());
// 遍歷顯示
for (it = lst.begin(); it != lst.end(); it++)
cout << *it << " "; // 輸出:8 9 9 9 2 4 5
cout << endl;
return 0;
}
3.3 刪除函數
- 頭部刪除元素:
lst.pop_front();
- 末尾刪除元素:
lst.pop_back();
- 任意位置刪除一個元素:
lst.erase(iterator it);
- 刪除 [first,last] 之間的元素:
lst.erase(iterator first, iterator last);
- 清空所有元素:
lst.clear();
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
list<int> lst;
for (int i = 0; i < 8; i++)
lst.push_back(i);
// 頭部刪除元素
lst.pop_front();
// 末尾刪除元素
lst.pop_back();
// 任意位置刪除一個元素
list<int>::iterator it = lst.begin();
lst.erase(it);
// 刪除[first,last]之間的元素
lst.erase(lst.begin(), ++lst.begin());
// 遍歷顯示
for (it = lst.begin(); it != lst.end(); it++)
cout << *it << " "; // 輸出:3 4 5 6
cout << endl;
// 清空所有元素
lst.clear();
// 判斷list是否為空
if (lst.empty())
cout << "元素為空" << endl; // 輸出:元素為空
return 0;
}
3.4 訪問函數
- 訪問第一個元素:
lst.front();
- 訪問最后一個元素:
lst.back();
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
list<int> lst;
for (int i = 0; i < 6; i++)
lst.push_back(i);
// 訪問第一個元素
cout << lst.front() << endl; // 輸出:0
// 訪問最后一個元素
cout << lst.back() << endl; // 輸出:5
return 0;
}
3.5 其他函數
- 多個元素賦值:
lst.assign(int nSize, const T& x); // 類似於初始化時用數組進行賦值
- 交換兩個同類型容器的元素:
swap(list&, list&); 或 lst.swap(list&);
- 合並兩個列表的元素(默認升序排列):
lst.merge();
- 在任意位置拼接入另一個list:
lst.splice(iterator it, list&);
- 刪除容器中相鄰的重復元素:
lst.unique();
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
// 多個元素賦值s
list<int> lst1;
lst1.assign(3, 1);
list<int> lst2;
lst2.assign(3, 2);
// 交換兩個容器的元素
// swap(lst1, lst2); // ok
lst1.swap(lst2);
// 遍歷顯示
cout << "交換后的lst1: ";
list<int>::iterator it;
for (it = lst1.begin(); it!=lst1.end(); it++)
cout << *it << " "; // 輸出:2 2 2
cout << endl;
// 遍歷顯示
cout << "交換后的lst2: ";
for (it = lst2.begin(); it != lst2.end(); it++)
cout << *it << " "; // 輸出:1 1 1
cout << endl;
list<int> lst3;
lst3.assign(3, 3);
list<int> lst4;
lst4.assign(3, 4);
// 合並兩個列表的元素
lst4.merge(lst3); // 不是簡單的拼接,而是會升序排列
cout << "合並后的lst4: ";
for (it = lst4.begin(); it != lst4.end(); it++)
cout << *it << " "; // 輸出:3 3 3 4 4 4
cout << endl;
list<int> lst5;
lst5.assign(3, 5);
list<int> lst6;
lst6.assign(3, 6);
// 在lst6的第2個元素處,拼接入lst5
lst6.splice(++lst6.begin(), lst5);
cout << "拼接后的lst6: ";
for (it = lst6.begin(); it != lst6.end(); it++)
cout << *it << " "; // 輸出:6 5 5 5 6 6
cout << endl;
// 刪除容器中相鄰的重復元素
list<int> lst7;
lst7.push_back(1);
lst7.push_back(1);
lst7.push_back(2);
lst7.push_back(2);
lst7.push_back(3);
lst7.push_back(2);
lst7.unique();
cout << "刪除容器中相鄰的重復元素后的lst7: ";
for (it = lst7.begin(); it != lst7.end(); it++)
cout << *it << " "; // 輸出:1 2 3 2
cout << endl;
return 0;
}
四、迭代器與算法
1. 迭代器
- 開始迭代器指針:
lst.begin();
- 末尾迭代器指針:
lst.end();
// 指向最后一個元素的下一個位置 - 指向常量的開始迭代器指針:
lst.cbegin();
// 意思就是不能通過這個指針來修改所指的內容,但還是可以通過其他方式修改的,而且指針也是可以移動的。 - 指向常量的末尾迭代器指針:
lst.cend();
- 反向迭代器指針,指向最后一個元素:
lst.rbegin();
- 反向迭代器指針,指向第一個元素的前一個元素:
lst.rend();
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
list<int> lst;
lst.push_back(1);
lst.push_back(2);
lst.push_back(3);
cout << *(lst.begin()) << endl; // 輸出:1
cout << *(--lst.end()) << endl; // 輸出:3
cout << *(lst.cbegin()) << endl; // 輸出:1
cout << *(--lst.cend()) << endl; // 輸出:3
cout << *(lst.rbegin()) << endl; // 輸出:3
cout << *(--lst.rend()) << endl; // 輸出:1
cout << endl;
return 0;
}
2. 算法
- 遍歷元素
list<int>::iterator it;
for (it = lst.begin(); it != lst.end(); it++)
cout << *it << endl;
- 元素翻轉
#include <algorithm>
reverse(lst.begin(), lst.end());
- 元素排序
#include <algorithm>
sort(lst.begin(), lst.end()); // 采用的是從小到大的排序
// 如果想從大到小排序,可以采用先排序后反轉的方式,也可以采用下面方法:
// 自定義從大到小的比較器,用來改變排序方式
bool Comp(const int& a, const int& b)
{
return a > b;
}
sort(lst.begin(), lst.end(), Comp);
五、總結
可以看到,list 與 vector、deque 的用法基本一致,除了以下幾處不同:
- list 為雙向迭代器,故不支持
it+=i
; - list 不支持下標訪問和at方法訪問。