定义于头文件 <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) 的要求。
赋值给容器
std::list<T,Allocator>::operator=
list& operator=( const list& other ); | (1) | |
list& operator=( list&& other ); | (2) | (C++11 起) (C++17 前) |
list& operator=( list&& other ) noexcept(/* see below */); | (C++17 起) | |
list& operator=( std::initializer_list<T> ilist ); | (3) | (C++11 起) |
替换容器内容。
1) 复制赋值运算符。以 other
的副本替换内容。若 std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value 为 true ,则以源分配器的副本替换目标分配器。若源分配器与目标分配器不比较相等,则用目标( *this )分配器销毁内存,然后在复制元素前用 other
的分配器分配。 (C++11 起).、
2) 移动赋值运算符。用移动语义以 other
的内容替换内容(即从 other
移动 other
中的数据到此容器)。之后 other
在合法但未指定的状态。若 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value 为 true ,则用源分配器的副本替换目标分配器。若它为 false 且源与目标分配器不比较相等,则目标不能取走源内存的所有权,而必须单独移动赋值逐个元素,用自己的分配器按需分配额外的内存。任何情况下,原先在 *this 中的元素要么被销毁,要么以逐元素移动赋值替换。
3) 以 initializer_list ilist
所标识者替换内容。
参数
other | - | 用作数据源的另一容器 |
ilist | - | 用作数据源的 initializer_list |
返回值
*this
复杂度
1) 与 *this
和 other
的大小成线性。
2) 与 *this
的大小成线性,除非分配器不比较相等且不传播,该情况下与 *this
和 other
的大小成线性。
3) 与 *this
和 ilist
的大小成线性。
异常2)noexcept 规定:noexcept(std::allocator_traits<Allocator>::is_always_equal::value) | (C++17 起) |
注意
容器移动赋值(重载 (2) )后,除非不兼容的分配器强制逐元素赋值,否则指向 other
的引用、指针和迭代器(除了尾迭代器)都保持合法,不过指代的元素现在在 *this 中。当前标准通过 §23.2.1[container.requirements.general]/12 中的总括陈述保证这点,而 LWG 2321 下正在考虑更直接的保证。
调用示例
#include <list>
#include <iostream>
void display_sizes(const std::list<int>& nums1,
const std::list<int>& nums2,
const std::list<int>& nums3)
{
std::cout << " nums1: " << nums1.size()
<< " nums2: " << nums2.size()
<< " nums3: " << nums3.size() << '\n';
}
int main()
{
std::list<int> nums1 {3, 1, 4, 6, 5, 9};
std::list<int> nums2;
std::list<int> nums3;
std::cout << "Initially:\n";
display_sizes(nums1, nums2, nums3);
// 复制赋值从 nums1 复制数据到 nums2
nums2 = nums1;
std::cout << "After assigment:\n";
display_sizes(nums1, nums2, nums3);
// 移动赋值从 nums1 移动数据到 nums3,
// 一同修改 nums1 和 nums3
nums3 = std::move(nums1);
std::cout << "After move assigment:\n";
display_sizes(nums1, nums2, nums3);
}
输出
将值赋给容器
std::list<T,Allocator>::assign
void assign( size_type count, const T& value ); | (1) | |
template< class InputIt > | (2) | |
void assign( std::initializer_list<T> ilist ); | (3) | (C++11 起) |
替换容器的内容。
1) 以 count
份 value
的副本替换内容。
2) 以范围 [first, last)
中元素的副本替换内容。若任一参数是指向 *this
中的迭代器则行为未定义。
若 | (C++11 前) |
此重载仅若 | (C++11 起) |
3) 以来自 initializer_list ilist
的元素替换内容。
所有指向容器元素的迭代器、指针及引用均被非法化。
参数
count | - | 容器的新大小 |
value | - | 用以初始化容器元素的值 |
first, last | - | 复制来源元素的范围 |
ilist | - | 复制值来源的 initializer_list |
复杂度
1) 与 count
成线性
2) 与 first
和 last
间的距离成线性
3) 与 ilist.size() 成线性
调用示例
#include <list>
#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()
{
std::list<char> characters;
characters.assign(5, 'a');
std::cout << "characters: " << characters << std::endl;
std::list<char> characters1;
characters1.assign({'a', 'b', 'c', 'd', 'e'});
std::cout << "characters1: " << characters1 << std::endl;
std::list<char> characters2;
characters2.assign(characters1.rbegin(), characters1.rend());
std::cout << "characters2: " << characters2 << std::endl;
return 0;
}