std::list
是支持常数时间从容器任何位置插入和移除元素的容器。不支持快速随机访问。它通常实现为双向链表。与 std::forward_list
相比,此容器提供双向迭代但在空间上效率稍低。
在 list 内或在数个 list 间添加、移除和移动元素不会非法化迭代器或引用。迭代器仅在对应元素被删除时非法化。
5.2 成员函数
5.2.1 修改器
函数 | 说明 |
---|---|
clear | 清除内容 (公开成员函数) |
insert | 插入元素 (公开成员函数) |
emplace (C++11) | 原位构造元素 (公开成员函数) |
erase | 擦除元素 (公开成员函数) |
push_back | 将元素添加到容器末尾 (公开成员函数) |
emplace_back (C++11) | 在容器末尾就地构造元素 (公开成员函数) |
pop_back | 移除末元素 (公开成员函数) |
push_front | 插入元素到容器起始 (公开成员函数) |
emplace_front (C++11) | 在容器头部就地构造元素 (公开成员函数) |
pop_front | 移除首元素 (公开成员函数) |
resize | 改变容器中可存储元素的个数 (公开成员函数) |
swap | 交换内容 (公开成员函数) |
- push_front
函数 | 说明 | |
---|---|---|
void push_front( const T& value ); | 前附给定元素 value 到容器起始。 | |
void push_front( T&& value ); | (C++11 起) |
没有引用和迭代器被非法化。
- pop_front
函数 | 说明 |
---|---|
void pop_front(); | 移除容器首元素。若容器中无元素,则行为未定义。 |
指向被擦除元素的迭代器和引用被非法化。
5.2.2 操作
函数 | 说明 |
---|---|
merge | 合并二个已排序列表 (公开成员函数) |
splice | 从另一个list 中移动元素 (公开成员函数) |
remove、remove_if | 移除满足特定标准的元素 (公开成员函数) |
reverse | 将该链表的所有元素的顺序反转 (公开成员函数) |
unique | 删除连续的重复元素 (公开成员函数) |
sort | 对元素进行排序 (公开成员函数) |
- merge
函数 | 说明 | |
---|---|---|
void merge( list& other ); | 归并二个已排序链表为一个。链表应以升序排序。 | |
void merge( list&& other ); | (C++11 起) | |
template void merge( list& other, Compare comp ); | ||
template void merge( list&& other, Compare comp ); | (C++11 起) |
不复制元素。操作后容器 other
变为空。若 other
与 *this 指代同一对象则函数不做任何事。
若 get_allocator() != other.get_allocator() ,则行为未定义。
没有引用和迭代器变得非法,除了被移动元素的迭代器现在指代到 *this 中,而非到 other
中,
第一版本用 operator< 比较元素,第二版本用给定的比较函数 comp
。
此操作是稳定的:对于二个链表中的等价元素,来自 *this
的元素始终前驱来自 other
的元素,而且 *this
和 other
的等价元素顺序不更改。
- splice
函数 | 说明 | |
---|---|---|
void splice( const_iterator pos, list& other ); | 从 other 转移所有元素到 *this 中。元素被插入到 pos 所指向的元素之前。操作后容器 other 变为空。若 other 与 *this 指代同一对象则行为未定义。 | |
void splice( const_iterator pos, list&& other ); | (C++11 起) | |
void splice( const_iterator pos, list& other, const_iterator it ); | 从 other 转移 it 所指向的元素到 *this 。元素被插入到 pos 所指向的元素之前。 | |
void splice( const_iterator pos, list&& other, const_iterator it ); | (C++11 起) | |
void splice( const_iterator pos, list& other, const_iterator first, const_iterator last); | 从 other 转移范围 [first, last) 中的元素到 *this 。元素被插入到 pos 所指向的元素之前。若 pos 是范围 [first,last) 中的迭代器则行为未定义。 | |
void splice( const_iterator pos, list&& other, const_iterator first, const_iterator last ); | (C++11 起) | |
从一个 list 转移元素给另一个。
不复制或移动元素,仅重指向链表结点的内部指针。若 get_allocator() != other.get_allocator() 则行为未定义。
没有迭代器或引用被非法化,指向被移动元素的迭代器保持合法,但现在指代到 *this 中,而非到 other
中。