定义:
list 是C++标准程式库中的一个类,可以简单视之为双向连结串列,以线性列的方式管理物件集合。list 的特色是在集合的任何位置增加或删除元素都很快,但是不支持随机存取。list 是C++标准程式库提供的众多容器(container)之一,除此之外还有vector、set、map、…等等。list 以模板方式实现(即泛型),可以处理任意型别的变数,包括使用者自定义的资料型态,例如:它可以是一个放置整数(int)型态的 list、也可以是放置字串(char 或 string)型态的 list、或者放置使用者自定类别(user-defined class)的 list。(来自维基百科)
接下来我们说一下它的用法
头文件
声明前首先需要头文件#include<list>。
构造
list(); |
(1) | |
(2) | ||
explicit list( size_type count, const T& value = T(),const Allocator& alloc = Allocator()); | (C++11 前) | |
list( size_type count, const T& value,const Allocator& alloc = Allocator()); | (C++11 起) | |
(3) | ||
explicit list( size_type count ); |
(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() ); |
(4) | |
list( const list& other ); |
(5) | |
list( const list& other, const Allocator& alloc ); |
(5) | (C++11 起) |
list( list&& other ); |
(6) | (C++11 起) |
list( list&& other, const Allocator& alloc ); |
(7) | (C++11 起) |
list( std::initializer_list<T> init, const Allocator& alloc = Allocator() ); |
(8) | (C++11 起) |
从各种数据源构造新容器,可选地使用用户提供的分配器 alloc
。
1) 默认构造函数。构造空容器。若不提供分配器,则从默认构造的实例获得分配器。
2) 构造拥有 count
个有值 value
的元素的容器。
3) 构造拥有个 count
默认插入的 T
实例的容器。不进行复制。
4) 构造拥有范围 [first, last)
内容的容器。
若 |
(C++11 前) |
此重载仅若 |
(C++11 起) |
5) 复制构造函数。构造拥有 other
内容的容器。若不提供 alloc
,则如同通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator())获得分配器。
6) 移动构造函数。用移动语义构造拥有 other
内容的容器。分配器通过属于 other
的分配器移动构造获得。
7) 有分配器扩展的移动构造函数。以 alloc
为新容器的分配器,从 other
移动内容;若 alloc != other.get_allocator() ,则它导致逐元素移动。
8) 构造拥有 initializer_list init
内容的容器。
参数
alloc | - | 用于此容器所有内存分配的分配器 |
count | - | 容器的大小 |
value | - | 以之初始化容器元素的值 |
first, last | - | 复制元素的来源范围 |
other | - | 用作初始化容器元素来源的另一容器 |
init | - | 用作初始化元素来源的 initializer_list |
复杂度
1) 常数
2-3) 与 count
成线性
4) 与 first
和 last
的距离成线性
5) 与 other
的大小成线性
6) 常数。
7) 若 alloc != other.get_allocator() 则为线性,否则为常数。
8) 与 init
的大小成线性。
成员函数
元素访问 |
|
访问第一个元素 (公开成员函数) |
|
访问最后一个元素 (公开成员函数) |
|
迭代器 |
|
返回指向容器第一个元素的迭代器 (公开成员函数) |
|
返回指向容器尾端的迭代器 (公开成员函数) |
|
返回指向容器最后元素的逆向迭代器 (公开成员函数) |
|
返回指向前端的逆向迭代器 (公开成员函数) |
|
容量 |
|
检查容器是否为空 (公开成员函数) |
|
返回容纳的元素数 (公开成员函数) |
|
返回可容纳的最大元素数 (公开成员函数) |
|
修改器 |
|
清除内容 (公开成员函数) |
|
插入元素 (公开成员函数) |
|
(C++11) |
原位构造元素 (公开成员函数) |
擦除元素 (公开成员函数) |
|
将元素添加到容器末尾 (公开成员函数) |
|
(C++11) |
在容器末尾就地构造元素 (公开成员函数) |
移除末元素 (公开成员函数) |
|
插入元素到容器起始 (公开成员函数) |
|
(C++11) |
在容器头部就地构造元素 (公开成员函数) |
移除首元素 (公开成员函数) |
|
改变容器中可存储元素的个数 (公开成员函数) |
|
交换内容 (公开成员函数) |
|
操作 |
|
合并二个已排序列表 (公开成员函数) |
|
从另一个list 中移动元素 (公开成员函数) |
|
移除满足特定标准的元素 (公开成员函数) |
|
将该链表的所有元素的顺序反转 (公开成员函数) |
|
删除连续的重复元素 (公开成员函数) |
|
对元素进行排序 (需要重载运算符) |
非成员函数
operator== |
按照字典顺序比较 list 中的值 (函数模板) |
特化 std::swap 算法 |
程序示例
#include <algorithm>
#include <iostream>
#include <list>
int main()
{
// 创建含整数的 list
std::list<int> l = { 7, 5, 16, 8 };
// 添加整数到 list 开头
l.push_front(25);
// 添加整数到 list 结尾
l.push_back(13);
// 以搜索插入 16 前的值
auto it = std::find(l.begin(), l.end(), 16);
if (it != l.end()) {
l.insert(it, 42);
}
// 迭代并打印 list 的值
for (int n : l) {
std::cout << n << '\n';
}
}