定义于: <forward_list>
请注意,本文尚未完成!您可以通过编辑此文档页面来提供帮助。
概述
- 简化版 (C++11起)
- 详细
template< class T, /* .. */ >
class forward_list;
- 常规版 (C++11起)
- 多态版 (C++17 起)
template<
class T,
class Allocator = std::allocator<T>
> class forward_list;
namespace pmr {
template< class T >
using forward_list = std::forward_list<T, std::pmr::polymorphic_allocator<T>>;
}
前向列表是一个支持在容器中任意位置快速插入和删除元素的容器。
前向列表的技术定义
std::forward_list
是一个支持在容器中任意位置快速插入和删除元素的容器。
不支持快速随机访问。
它被实现为单向链表。与std::list
相比,当不需要双向迭代时,此容器提供更节省空间的存储。
在列表中或跨多个列表添加、删除和移动元素不会使当前指向列表中其他元素的迭代器失效。但是,当相应元素从列表中删除时(通过erase_after
),指向该元素的迭代器或引用将失效。
std::forward_list
满足Container
(除了size成员函数和operator==
的复杂度总是线性的)、AllocatorAwareContainer
和SequenceContainer
的要求。
std::forward_list
定义于 | forward_list |
模板参数
有关更多详细信息,请参阅技术细节。
pub | T | 元素的类型。 |
pub | Allocator | 用于获取/释放内存并在该内存中构造/销毁元素的分配器。 |
类型名称
pub | value_type | T |
pub | allocator_type | Allocator |
pub | size_type | 无符号整型(通常是std::size_t ) |
pub | difference_type | 有符号整型(通常是std::ptrdiff_t ) |
pub | reference | value_type& |
pub | const_reference | value_type const& |
pub | pointer | Allocator::pointer (直到 C++11)std::allocator_traits<Allocator>::pointer (自 C++11 起) |
pub | const_pointer | Allocator::const_pointer (直到 C++11)std::allocator_traits<Allocator>::const_pointer (自 C++11 起) |
pub | iterator |
|
pub | const_iterator |
|
成员函数
pub | (构造函数) | 构造一个向量。 |
pub | (析构函数) | 销毁向量,如果使用则释放内部存储。 |
pub | operator= | 将值分配给容器。 |
pub | assign | 将值分配给容器。 |
pub | get_allocator | 返回关联的分配器。 |
元素访问
pub | front | 访问第一个元素。 |
迭代器
pub | before_begin cbefore_begin | 返回指向起始元素之前的 |
pub | begin cbegin | 返回指向起始元素的 |
pub | end cend | 返回指向结束元素的 |
容量
pub | empty | 如果列表为空,则返回 |
pub | max_size | 返回最大可能的元素数量。 |
修饰符
pub | clear | 清空内容。 |
pub | insert_after | 在元素之后插入元素。 |
pub | emplace_after | 在元素之后就地构造元素。 |
pub | erase_after | 在元素之后擦除一个元素。 |
pub | push_front | 在开头插入一个元素。 |
pub | emplace_front | 在开头就地构造一个元素。 |
pub | pop_front | 移除第一个元素。 |
pub | resize | 更改存储的元素数量。 |
pub | swap | 交换内容。 |
操作
pub | merge | 合并两个已排序的列表。 |
pub | splice_after | 从另一个前向列表移动元素。 |
pub | remove remove_if | 移除满足特定条件的元素。 |
pub | reverse | 反转元素的顺序。 |
pub | unique | 移除连续的重复元素。 |
pub | sort | 排序元素。 |
非成员函数
pub | operator== operator!= (在 C++20 中移除) operator< (在 C++20 中移除) operator> (在 C++20 中移除) operator<= (在 C++20 中移除) operator>= (在 C++20 中移除) operator<=> (自 C++20 起) | 按字典顺序比较列表中的值。 |
pub | std::swap (std::forward_list) | std::swap 算法的重载。 |
pub | erase (std::forward_list) erase_if (std::forward_list) | std::erase/std::erase_if 算法的重载。 |
推导指南 (C++17 起)
点击展开
推导指南
// (1)
template< class InputIt,
class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>>
forward_list(InputIt, InputIt, Alloc = Alloc())
-> forward_list<typename std::iterator_traits<InputIt>::value_type, Alloc>;
(1)
允许从**迭代器范围**进行推导。
重载决议
为了使任何推导指南参与重载决议,必须满足以下要求
InputIt
满足LegacyInputIterator
的要求Alloc
满足Allocator
的要求
库确定类型不满足 LegacyInputIterator
的程度是未指定的,但至少
- 整型不符合输入迭代器的条件。
同样,它确定类型不满足 Allocator
的程度是未指定的,但至少
- 成员类型
Alloc::value_type
必须存在。 - 当作为未求值操作数处理时,表达式
std::declval<Alloc&>().allocate(std::size_t{})
必须是格式良好的。
示例
本节需要改进。您可以通过编辑此文档页面来帮助我们。