std::list
定义于头文件 <list> |
||
template< class T, |
(1) | |
namespace pmr { template< class T > |
(2) | (自 C++17 起) |
std::list
是一个容器,它支持在容器中任何位置的常数时间插入和移除元素。不支持快速随机访问。它通常实现为双向链表。与 std::forward_list 相比,此容器提供双向迭代能力,但空间效率较低。
在列表内部或跨多个列表添加、移除和移动元素不会使迭代器或引用失效。迭代器仅在删除相应元素时失效。
std::list
满足 容器 (Container)、 分配器感知容器 (AllocatorAwareContainer)、 顺序容器 (SequenceContainer) 和 可逆容器 (ReversibleContainer) 的要求。
|
(自 C++26 起) |
内容 |
[编辑] 模板参数
T | - | 元素的类型。
| ||||||||||||||
Allocator | - | 用于获取/释放内存以及在该内存中构造/销毁元素的分配器。该类型必须满足 分配器 (Allocator) 的要求。行为是未定义的(C++20 前)程序是非良构的(自 C++20 起) 如果 Allocator::value_type 与 T 不同。 |
[编辑] 成员类型
成员类型 | 定义 | ||||
value_type
|
T | ||||
allocator_type
|
Allocator | ||||
size_type
|
无符号整数类型(通常为 std::size_t) | ||||
difference_type
|
带符号整数类型(通常为 std::ptrdiff_t) | ||||
reference
|
value_type& | ||||
const_reference
|
const value_type& | ||||
pointer
|
| ||||
const_pointer
|
| ||||
iterator
|
旧式双向迭代器 (LegacyBidirectionalIterator) 和 ConstexprIterator(自 C++26 起) 到 value_type | ||||
const_iterator
|
旧式双向迭代器 (LegacyBidirectionalIterator) 和 ConstexprIterator(自 C++26 起) 到 const value_type | ||||
reverse_iterator
|
std::reverse_iterator<iterator> | ||||
const_reverse_iterator
|
std::reverse_iterator<const_iterator> |
[编辑] 成员函数
构造 list (公有成员函数) | |
析构 list (公有成员函数) | |
为容器赋值 (公有成员函数) | |
为容器赋值 (公有成员函数) | |
(C++23) |
为容器赋值一个值范围 (公有成员函数) |
返回关联的分配器 (公有成员函数) | |
元素访问 | |
访问首元素 (公有成员函数) | |
访问尾元素 (公有成员函数) | |
迭代器 | |
(C++11) |
返回指向开头的迭代器 (公有成员函数) |
(C++11) |
返回指向末尾的迭代器 (公有成员函数) |
(C++11) |
返回指向开头的逆向迭代器 (公有成员函数) |
(C++11) |
返回指向末尾的逆向迭代器 (公有成员函数) |
容量 | |
检查容器是否为空 (公有成员函数) | |
返回元素数量 (公有成员函数) | |
返回最大可能的元素数量 (公有成员函数) | |
修改器 | |
清除内容 (公有成员函数) | |
插入元素 (公有成员函数) | |
(C++23) |
插入一个元素范围 (公有成员函数) |
(C++11) |
就地构造元素 (公有成员函数) |
擦除元素 (公有成员函数) | |
在末尾添加一个元素 (公有成员函数) | |
(C++11) |
在末尾就地构造一个元素 (公有成员函数) |
(C++23) |
在末尾添加一个元素范围 (公有成员函数) |
移除最后一个元素 (公有成员函数) | |
在开头插入一个元素 (公有成员函数) | |
(C++11) |
在开头就地构造一个元素 (公有成员函数) |
(C++23) |
在开头添加一个元素范围 (公有成员函数) |
移除第一个元素 (公有成员函数) | |
更改存储的元素数量 (公有成员函数) | |
交换内容 (公有成员函数) | |
操作 | |
合并两个已排序的列表 (公有成员函数) | |
从另一个 list 移动元素(公有成员函数) | |
移除满足特定条件的元素 (公有成员函数) | |
反转元素的顺序 (公有成员函数) | |
移除连续的重复元素 (公有成员函数) | |
排序元素 (公有成员函数) |
[编辑] 非成员函数
(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(C++20) |
按字典序比较两个 list 的值(函数模板) |
特化 std::swap 算法 (函数模板) | |
擦除所有满足特定条件的元素 (函数模板) |
推导指引 |
(自 C++17 起) |
[编辑] 注解
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 容器的范围构造和插入 |
__cpp_lib_constexpr_containers |
202502L |
(C++26) | constexpr std::list |
[编辑] 示例
#include <algorithm> #include <iostream> #include <list> int main() { // Create a list containing integers std::list<int> l = {7, 5, 16, 8}; // Add an integer to the front of the list l.push_front(25); // Add an integer to the back of the list l.push_back(13); // Insert an integer before 16 by searching auto it = std::find(l.begin(), l.end(), 16); if (it != l.end()) l.insert(it, 42); // Print out the list std::cout << "l = { "; for (int n : l) std::cout << n << ", "; std::cout << "};\n"; }
输出
l = { 25, 7, 5, 42, 16, 8, 13, };
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布的行为 | 正确的行为 |
---|---|---|---|
LWG 230 | C++98 | T 不需要是 可复制构造 (CopyConstructible)( T 类型的元素可能无法构造) |
T 也需要是可复制构造 (CopyConstructible) |
LWG 276 | C++98 | T 始终需要是 可复制赋值 (CopyAssignable) |
仅当 operator= 或 assign 使用 T 实例化时才需要 |
[编辑] 参见
(C++11) |
单向链表 (类模板) |