std::list定义于头文件<list>
std::list是支持常数时间从容器任何位置插入和移除元素的容器。它实现为双向链表,list不支持快速随机访问。与实现为单向链表的std::forward_list相比,std::list提供双向迭代但在空间上效率稍低。
其成员类型有:
成员类型 | 定义 |
value_type | T |
allocator_type | Allocator |
size_type | 无符号整数类型(通常是 std::size_t ) |
difference_type | 有符号整数类型(通常是 std::ptrdiff_t ) |
reference | Allocator::reference (C++11 前) value_type& (C++11 起) |
const_reference | Allocator::const_reference (C++11 前) const value_type& (C++11 起) |
pointer | Allocator::pointer (C++11 前) std::allocator_traits<Allocator>::pointer (C++11 起) |
const_pointer | Allocator::const_pointer (C++11 前) std::allocator_traits<Allocator>::const_pointer (C++11 起) |
iterator | 遗留双向迭代器 (LegacyBidirectionalIterator) |
const_iterator | 常双向迭代器 |
reverse_iterator | std::reverse_iterator<iterator> |
const_reverse_iterator | std::reverse_iterator<const_iterator> |
成员函数
构造函数:有默认构造函数,支持copy constructor, move constructor 和 列表初始化构造
析构函数:默认析构函数
迭代器相关的函数:(c 指const )
begin() cbegin() | 指向起始的迭代器 |
end() cend() | 指向末尾的迭代器 |
rbegin() crbegin() | 指向起始的逆向迭代器 |
rend() crend() | 指向末尾的逆向迭代器 |
容量大小相关的函数
empty() | 容器是否为空即(begin() == end() ?) |
size() | 返回元素的数量 |
max_size() | 返回可容纳的最大元素数量 |
resize() | 改变容器可容纳元素的数量 |
元素访问
front() | 访问第一个元素 |
back() | 访问最后一个元素 |
容器操作:
push_front() | 将元素添加到容器头部 |
emplace_front() | 在容器头部构造元素 |
pop_front() | 删除第一个元素 |
push_back() | 将元素添加到容器末尾 |
emplace_back() | 在容器尾部构造元素 |
pop_back() | 删除最后一个元素 |
emplace() | 原位构造元素 |
insert() | 插入元素 |
earse() | 擦除元素 |
swap() | 与另外一个list交换内容 |
clear() | 清空元素 |
list操作:
splice() | 从另外一个list中移动元素 |
remove() remove_if() | 删除满足条件的元素 |
unique() | 删除连续重复的元素 |
merge() | 合并排好序的两个链表 |
sort() | 对链表排序 |
非成员函数
operator== operator!= (C++20 中移除) operator< (C++20 中移除) operator<= (C++20 中移除) operator> (C++20 中移除) operator>= (C++20 中移除) operator<=>(C++20) | 按照字典顺序比较 list 中的值 (函数模板) |
std::swap(std::list) | 特化 std::swap 算法 (函数模板) |
erase(std::list)erase_if(std::list) (C++20) | 擦除所有满足特定判别标准的元素 (函数模板) |
作为链表的实现,list拥有自己的sort函数,而不能使用std::sort函数进行排序。std::sort使用的随机访问来进行排序,而list和forward_list都是不支持随机访问的。
示例如下:
#include <iostream>
#include <list>
#include <algorithm>
int main() {
std::list<int> li{8,6,4,5,2,3,1};
std::list<int> li2;
std::cout << std::boolalpha << li2.empty() << std::endl;
li.swap(li2);
std::cout << "li2 size : " << li2.size() << std::endl;
std::cout << "li size : " << li.size() << std::endl;
std::cout << "li max_size: " << li.max_size() << std::endl;
std::for_each(li2.begin(),li2.end(),[](int x){std::cout << x << 't';});
std::cout << 'n';
li2.sort();
std::for_each(li2.begin(),li2.end(),[](int x){std::cout << x << 't';});
std::cout << 'n';
li.splice(li.begin(),li2);
std::cout << "li size : " << li.size() << std::endl;
std::cout << "li max_size: " << li.max_size() << std::endl;
std::cout << "li2 size : " << li2.size() << std::endl;
li.pop_back();
li.pop_front();
li.push_back(100);
li.push_front(200);
li.emplace_back(1000);
li.emplace_front(20);
li.emplace(++li.begin(),1050);
std::for_each(li.begin(),li.end(),[](int x){std::cout << x << 't';});
std::cout << 'n';
std::cout << "front : " << li.front() << std::endl;
std::cout << "back : " << li.back() << std::endl;
li.insert(++li.begin(),2);
li.insert(++li.begin(),2);
std::for_each(li.begin(),li.end(),[](int x){std::cout << x << 't';});
std::cout << 'n';
li2.push_front(10);
li2.push_front(63);
li.merge(li2);
std::for_each(li.begin(),li.end(),[](int x){std::cout << x << 't';});
std::cout << 'n';
std::cout << "li2 size : " << li2.size() << std::endl;
li.unique();
li.remove(63);
std::for_each(li.begin(),li.end(),[](int x){std::cout << x << 't';});
std::cout << 'n';
//去除偶数
li.remove_if([](int x){return x%2==0;});
std::for_each(li.begin(),li.end(),[](int x){std::cout << x << 't';});
std::cout << 'n';
li.clear();
return 0;
}