版权声明:本文为博主原创文章,博客地址:https://blog.csdn.net/qq_41855420,未经博主允许不得转载。 https://blog.csdn.net/qq_41855420/article/details/89764907
C++ list 容器
定义于头文件 <list>
申明 template<class T, class Allocator = std::allocator<T> > class list;
std::list 是支持常数时间从容器任何位置插入和移除元素的容器。不支持快速随机访问。它通常实现为双向链表。与 std::forward_list 相比,此容器提供双向迭代但在空间上效率稍低。
在 list 内或在数个 list 间添加、移除和移动元素不会非法化迭代器或引用。迭代器仅在对应元素被删除时非法化。
std::list 满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、顺序容器 (SequenceContainer) 及可逆容器 (ReversibleContainer) 的要求。
模板参数
T - 元素的类型。
- T 必须满足可拷贝赋值 (CopyAssignable) 和可拷贝构造 (CopyConstructible) 的要求。 (C++11
前) - 加诸元素的要求依赖于容器上进行的实际操作。泛言之,要求元素类型是完整类型并满足可擦除 (Erasable)
的要求,但许多成员函数附带了更严格的要求。 (C++11 起) - 加诸元素的要求依赖于容器上进行的实际操作。泛言之,要求元素类型满足可擦除 (Erasable)
的要求,但许多成员函数附带了更严格的要求。若分配器满足分配器完整性要求,则容器(但非其成员)能以不完整元素类型实例化。 (C++17
起)
Allocator - 用于获取/释放内存及构造/析构内存中元素的分配器。类型必须满足分配器 (Allocator) 的要求。若 Allocator::value_type 与 T 不同则行为未定义。
成员类型
成员类型 | 定义 |
---|---|
value_type | T |
allocator_type | Allocator |
size_type | 无符号整数类型(通常是 std::size_t ) |
difference_type | 有符号整数类型(通常是 std::ptrdiff_t ) |
reference | Allocator::reference (C++11 前) value_type& (C++11 起) |
const_reference | Allocator::const_reference (C++11 前) const value_type& (C++11 起) |
pointer | Allocator::pointer (C++11 前) std::allocator_traits::pointer (C++11 起) |
const_pointer | Allocator::const_pointer (C++11 前) std::allocator_traits::const_pointer (C++11 起) |
iterator | 双向迭代器 (LegacyBidirectionalIterator) |
const_iterator | 常双向迭代器 |
reverse_iterator | std::reverse_iterator |
const_reverse_iterator | std::reverse_iterator<const_iterator> |
成员函数
(构造函数) | 构造 list (公开成员函数) |
(析构函数) | 析构 list (公开成员函数) |
operator= | 赋值给容器 (公开成员函数) |
assign | 将值赋给容器 (公开成员函数) |
get_allocator | 返回相关的分配器 (公开成员函数) |
元素访问
front | 访问第一个元素 (公开成员函数) |
back | 访问最后一个元素 (公开成员函数) |
迭代器
begin、cbegin | 返回指向容器第一个元素的迭代器 (公开成员函数) |
end、cend | 返回指向容器尾端的迭代器 (公开成员函数) |
rbegin、crbegin | 返回指向容器最后元素的逆向迭代器 (公开成员函数) |
rend、crend | 返回指向前端的逆向迭代器 (公开成员函数) |
容量
empty | 检查容器是否为空 (公开成员函数) |
size | 返回容纳的元素数 (公开成员函数) |
max_size | 返回可容纳的最大元素数 (公开成员函数) |
修改器
clear | 清除内容 (公开成员函数) |
insert | 插入元素 (公开成员函数) |
emplace | (C++11) 原位构造元素 (公开成员函数) |
erase | 擦除元素 (公开成员函数) |
push_back | 将元素添加到容器末尾 (公开成员函数) |
emplace_back (C++11) | 在容器末尾就地构造元素 (公开成员函数) |
pop_back | 移除末元素 (公开成员函数) |
push_front | 插入元素到容器起始 (公开成员函数) |
emplace_front(C++11) | 在容器头部就地构造元素 (公开成员函数) |
pop_front | 移除首元素 (公开成员函数) |
resize | 改变容器中可存储元素的个数 (公开成员函数) |
swap | 交换内容 (公开成员函数) |
操作
merge | 合并二个已排序列表 (公开成员函数) |
splice | 从另一个list中移动元素 (公开成员函数) |
remove、remove_if | 移除满足特定标准的元素 (公开成员函数) |
reverse | 将该链表的所有元素的顺序反转 (公开成员函数) |
unique | 删除连续的重复元素 (公开成员函数) |
sort | 对元素进行排序 (公开成员函数) |
示例
#include <algorithm>
#include <iostream>
#include <list>
int main(){
// 创建含整数的 list
std::list<int> l = { 7, 5, 16, 8 };
// 添加整数到 list 开头
l.push_front(25);
// 添加整数到 list 结尾
l.push_back(13);
// 以搜索插入 16 前的值
auto it = std::find(l.begin(), l.end(), 16);
if (it != l.end()) {
l.insert(it, 42);
}
// 迭代并打印 list 的值
for (int n : l) {
std::cout << n << '\n';
}
return 0;
}
输出:
25
7
5
42
16
8
13