std::list

std::list 是支持常数时间从容器任何位置插入和移除元素的容器。不支持快速随机访问。它通常实现为双向链表。与 std::forward_list相比,此容器提供双向迭代但在空间上效率稍低。

在 list 内或在数个 list 间添加、移除和移动元素不会非法化迭代器或引用。迭代器仅在对应元素被删除时非法化。

5.2 成员函数

5.2.1 修改器
函数说明
clear清除内容 (公开成员函数)
insert插入元素 (公开成员函数)
emplace (C++11)原位构造元素 (公开成员函数)
erase擦除元素 (公开成员函数)
push_back将元素添加到容器末尾 (公开成员函数)
emplace_back (C++11)在容器末尾就地构造元素 (公开成员函数)
pop_back移除末元素 (公开成员函数)
push_front插入元素到容器起始 (公开成员函数)
emplace_front (C++11)在容器头部就地构造元素 (公开成员函数)
pop_front移除首元素 (公开成员函数)
resize改变容器中可存储元素的个数 (公开成员函数)
swap交换内容 (公开成员函数)
  • push_front
函数说明
void push_front( const T& value );前附给定元素 value 到容器起始。
void push_front( T&& value );(C++11 起)

没有引用和迭代器被非法化。

  • pop_front
函数说明
void pop_front();移除容器首元素。若容器中无元素,则行为未定义。

指向被擦除元素的迭代器和引用被非法化。

5.2.2 操作
函数说明
merge合并二个已排序列表 (公开成员函数)
splice从另一个list中移动元素 (公开成员函数)
remove、remove_if移除满足特定标准的元素 (公开成员函数)
reverse将该链表的所有元素的顺序反转 (公开成员函数)
unique删除连续的重复元素 (公开成员函数)
sort对元素进行排序 (公开成员函数)
  • merge
函数说明
void merge( list& other );归并二个已排序链表为一个。链表应以升序排序。
void merge( list&& other );(C++11 起)
template void merge( list& other, Compare comp );
template void merge( list&& other, Compare comp );(C++11 起)

不复制元素。操作后容器 other 变为空。若 other 与 *this 指代同一对象则函数不做任何事。

若 get_allocator() != other.get_allocator() ,则行为未定义。

没有引用和迭代器变得非法,除了被移动元素的迭代器现在指代到 *this 中,而非到 other 中,

第一版本用 operator< 比较元素,第二版本用给定的比较函数 comp

此操作是稳定的:对于二个链表中的等价元素,来自 *this 的元素始终前驱来自 other 的元素,而且 *thisother 的等价元素顺序不更改。

  • splice
函数说明
void splice( const_iterator pos, list& other );other 转移所有元素到 *this 中。元素被插入到 pos 所指向的元素之前。操作后容器 other 变为空。若 other 与 *this 指代同一对象则行为未定义。
void splice( const_iterator pos, list&& other );(C++11 起)
void splice( const_iterator pos, list& other, const_iterator it );other 转移 it 所指向的元素到 *this 。元素被插入到 pos 所指向的元素之前。
void splice( const_iterator pos, list&& other, const_iterator it );(C++11 起)
void splice( const_iterator pos, list& other, const_iterator first, const_iterator last);other 转移范围 [first, last) 中的元素到 *this 。元素被插入到 pos 所指向的元素之前。若 pos 是范围 [first,last) 中的迭代器则行为未定义。
void splice( const_iterator pos, list&& other, const_iterator first, const_iterator last );(C++11 起)

从一个 list 转移元素给另一个。

不复制或移动元素,仅重指向链表结点的内部指针。若 get_allocator() != other.get_allocator() 则行为未定义。

没有迭代器或引用被非法化,指向被移动元素的迭代器保持合法,但现在指代到 *this 中,而非到 other 中。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值