c++标准库中的list容器是基于双向链表实现的序列式容器,它的优势在于任意位置插入和删除元素的时间复杂度都是O(1),不需要像vector那样移动大量元素,适合频繁进行增删操作的场景。

list容器的基础使用准备
使用list容器前需要包含对应的头文件,并且声明对应的命名空间,基础的使用准备代码如下:
#include <iostream>
#include <list>
using namespace std;
int main() {
// 声明一个存储int类型的list容器
list<int> my_list;
return 0;
}
list容器插入元素的方法
1. 尾部插入元素
使用push_back方法可以在list容器的尾部添加新元素,代码如下:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> my_list;
// 尾部插入3个元素
my_list.push_back(10);
my_list.push_back(20);
my_list.push_back(30);
// 遍历输出元素
for (int num : my_list) {
cout << num << " ";
}
// 输出结果为 10 20 30
return 0;
}
2. 头部插入元素
使用push_front方法可以在list容器的头部添加新元素,代码如下:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> my_list;
my_list.push_back(10);
my_list.push_back(20);
// 头部插入元素5
my_list.push_front(5);
for (int num : my_list) {
cout << num << " ";
}
// 输出结果为 5 10 20
return 0;
}
3. 指定位置插入元素
使用insert方法可以在迭代器指向的位置插入元素,该方法返回指向新插入元素的迭代器,代码如下:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> my_list;
my_list.push_back(10);
my_list.push_back(30);
// 获取指向第一个元素的迭代器
auto it = my_list.begin();
// 在第一个元素位置插入20
my_list.insert(it, 20);
for (int num : my_list) {
cout << num << " ";
}
// 输出结果为 20 10 30
return 0;
}
4. 批量插入元素
insert方法还支持批量插入多个相同元素,或者插入另一个容器的元素区间,代码如下:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> my_list = {1, 2};
// 批量插入3个5
my_list.insert(my_list.end(), 3, 5);
list<int> other_list = {6, 7};
// 插入other_list的所有元素
my_list.insert(my_list.end(), other_list.begin(), other_list.end());
for (int num : my_list) {
cout << num << " ";
}
// 输出结果为 1 2 5 5 5 6 7
return 0;
}
list容器删除元素的方法
1. 尾部删除元素
使用pop_back方法可以删除list容器尾部的元素,代码如下:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> my_list = {10, 20, 30};
// 删除尾部元素
my_list.pop_back();
for (int num : my_list) {
cout << num << " ";
}
// 输出结果为 10 20
return 0;
}
2. 头部删除元素
使用pop_front方法可以删除list容器头部的元素,代码如下:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> my_list = {10, 20, 30};
// 删除头部元素
my_list.pop_front();
for (int num : my_list) {
cout << num << " ";
}
// 输出结果为 20 30
return 0;
}
3. 删除指定位置的元素
使用erase方法可以删除迭代器指向的单个元素,或者删除一个区间内的所有元素,删除后返回指向被删除元素后一个位置的迭代器,代码如下:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> my_list = {10, 20, 30, 40};
auto it = my_list.begin();
// 移动到第二个元素位置
++it;
// 删除第二个元素20
my_list.erase(it);
// 删除前两个元素
auto start = my_list.begin();
auto end = my_list.begin();
advance(end, 2);
my_list.erase(start, end);
for (int num : my_list) {
cout << num << " ";
}
// 输出结果为 40
return 0;
}
4. 删除所有匹配的元素
使用remove方法可以删除容器中所有的指定值元素,代码如下:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> my_list = {10, 20, 10, 30, 10};
// 删除所有值为10的元素
my_list.remove(10);
for (int num : my_list) {
cout << num << " ";
}
// 输出结果为 20 30
return 0;
}
5. 清空所有元素
使用clear方法可以删除list容器中的所有元素,代码如下:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> my_list = {10, 20, 30};
// 清空所有元素
my_list.clear();
cout << "容器元素个数:" << my_list.size() << endl;
// 输出结果为 容器元素个数:0
return 0;
}
操作注意事项
list容器的迭代器在插入元素时不会失效,但是删除元素时,被删除元素对应的迭代器会失效,后续不能再使用该迭代器。如果需要遍历删除元素,建议使用erase方法的返回值更新迭代器,避免迭代器失效导致的程序错误。