定义于头文件 <list>
template< class T, class Allocator = std::allocator<T> > class list; | (1) | |
namespace pmr { template <class T> using list = std::list<T, std::pmr::polymorphic_allocator<T>>;} | (2) | (C++17 起) |
std::list
是支持常数时间从容器任何位置插入和移除元素的容器。不支持快速随机访问。它通常实现为双向链表。与 std::forward_list 相比,此容器提供双向迭代但在空间上效率稍低。
在 list 内或在数个 list 间添加、移除和移动元素不会非法化迭代器或引用。迭代器仅在对应元素被删除时非法化。
std::list
满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、序列容器 (SequenceContainer) 及可逆容器 (ReversibleContainer) 的要求。
模板参数
T | - | 元素的类型。
| ||||||
Allocator | - | 用于获取/释放内存及构造/析构内存中元素的分配器。类型必须满足分配器 (Allocator) 的要求。若 Allocator::value_type 与 T 不同则行为未定义。 |
成员类型
成员类型 | 定义 | ||||
value_type | T | ||||
allocator_type | Allocator | ||||
size_type | 无符号整数类型(通常是 std::size_t ) | ||||
difference_type | 有符号整数类型(通常是 std::ptrdiff_t ) | ||||
reference |
| ||||
const_reference |
| ||||
pointer |
| ||||
const_pointer |
| ||||
iterator | 遗留双向迭代器 (LegacyBidirectionalIterator) | ||||
const_iterator | 常双向迭代器 | ||||
reverse_iterator | std::reverse_iterator<iterator> | ||||
const_reverse_iterator | std::reverse_iterator<const_iterator> |
构造函数
std::list<T,Allocator>::list
list(); | (1) | |
explicit list( const Allocator& alloc ); | (2) | |
explicit list( size_type count, const T& value = T(), const Allocator& alloc = Allocator()); | (3) | (C++11 前) |
list( size_type count, const T& value, const Allocator& alloc = Allocator()); | (C++11 起) | |
explicit list( size_type count ); | (4) | (C++11 起) (C++14 前) |
explicit list( size_type count, const Allocator& alloc = Allocator() ); | (C++14 起) | |
template< class InputIt > list( InputIt first, InputIt last, const Allocator& alloc = Allocator() ); | (5) | |
list( const list& other ); | (6) | |
list( const list& other, const Allocator& alloc ); | (6) | (C++11 起) |
list( list&& other ); | (7) | (C++11 起) |
list( list&& other, const Allocator& alloc ); | (8) | (C++11 起) |
list( std::initializer_list<T> init, | (9) | (C++11 起) |
从各种数据源构造新容器,可选地使用用户提供的分配器 alloc
。
1) 默认构造函数。构造拥有默认构造的分配器的空容器。
2) 构造拥有给定分配器 alloc
的空容器。
3) 构造拥有 count
个有值 value
的元素的容器。
4) 构造拥有个 count
默认插入的 T
实例的容器。不进行复制。
5) 构造拥有范围 [first, last)
内容的容器。
若 | (C++11 前) |
此重载仅若 | (C++11 起) |
6) 复制构造函数。构造拥有 other
内容的容器。若不提供 alloc
,则如同通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 获得分配器。
7) 移动构造函数。用移动语义构造拥有 other
内容的容器。分配器通过属于 other
的分配器移动构造获得。
8) 有分配器扩展的移动构造函数。以 alloc
为新容器的分配器,从 other
移动内容;若 alloc != other.get_allocator() ,则它导致逐元素移动。
9) 构造拥有 initializer_list init
内容的容器。
参数
alloc | - | 用于此容器所有内存分配的分配器 |
count | - | 容器的大小 |
value | - | 以之初始化容器元素的值 |
first, last | - | 复制元素的来源范围 |
other | - | 用作初始化容器元素来源的另一容器 |
init | - | 用作初始化元素来源的 initializer_list |
复杂度
1-2) 常数
3-4) 与 count
成线性
5) 与 first
和 last
的距离成线性
6) 与 other
的大小成线性
7) 常数。
8) 若 alloc != other.get_allocator() 则为线性,否则为常数。
9) 与 init
的大小成线性。
异常
到 Allocator::allocate
的调用可能抛出。
调用示例
#include <list>
#include <string>
#include <iostream>
template<typename T>
std::ostream& operator<<(std::ostream& s, const std::list<T>& v)
{
s.put('[');
char comma[3] = {'\0', ' ', '\0'};
for (const auto& e : v)
{
s << comma << e;
comma[0] = ',';
}
return s << ']';
}
int main()
{
// c++11 初始化器列表语法:
std::list<std::string> words1 {"the", "frogurt", "is", "also", "cursed"};
std::cout << "words1: " << words1 << '\n';
// words2 == words1
std::list<std::string> words2(words1.begin(), words1.end());
std::cout << "words2: " << words2 << '\n';
// words3 == words1
std::list<std::string> words3(words1);
std::cout << "words3: " << words3 << '\n';
// words4 为 {"Mo", "Mo", "Mo", "Mo", "Mo"}
std::list<std::string> words4(5, "Mo");
std::cout << "words4: " << words4 << '\n';
}
输出
析构函数
std::list<T,Allocator>::~list
~list();
销毁容器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。
复杂度
与容器大小成线性。