std::list<T,Allocator>::splice
来自 cppreference.cn
void splice( const_iterator pos, list& other ); |
(1) | |
void splice( const_iterator pos, list&& other ); |
(2) | (C++11 起) |
void splice( const_iterator pos, list& other, const_iterator it ); |
(3) | |
void splice( const_iterator pos, list&& other, const_iterator it ); |
(4) | (C++11 起) |
void splice( const_iterator pos, list& other, const_iterator first, const_iterator last); |
(5) | |
void splice( const_iterator pos, list&& other, const_iterator first, const_iterator last ); |
(6) | (C++11 起) |
将元素从一个列表转移到另一个列表。
没有元素被复制或移动,只有列表节点的内部指针被重新指向。没有迭代器或引用失效,指向被移动元素的迭代器仍然有效,但现在它们指向 *this,而不是 other。
1,2) 将 other 中的所有元素转移到 *this。这些元素插入到 pos 所指向的元素之前。操作后,容器 other 变为空。
3,4) 将 other 中由 it 所指向的元素转移到 *this。该元素插入到 pos 所指向的元素之前。
5,6) 将范围
[
first,
last)
中的元素从 other 转移到 *this。这些元素插入到 pos 所指向的元素之前。若出现以下情况,行为未定义:
- get_allocator() != other.get_allocator(),则无法保证 O(1) 节点移动,
- 对于重载 (1,2),*this 和 other 指的是同一个对象,
- 对于重载 (3,4),it 不是 other 中可解引用迭代器,或者
- 对于重载 (5,6),
-
[
first,
last)
不是 other 中的有效范围,或者 - pos 位于
[
first,
last)
中。
-
目录 |
[编辑] 参数
pos | - | 内容将被插入到的元素之前的位置 |
其他 | - | 另一个要从中转移内容的容器 |
it | - | 要从 other 转移到 *this 的元素 |
first, last | - | 定义要从 other 转移到 *this 的元素范围的迭代器对 |
[编辑] 返回值
(无)
[编辑] 异常
不抛出任何异常。
[编辑] 复杂度
1-4) 常数时间。
[编辑] 示例
运行此代码
#include <iostream> #include <list> std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list) { for (auto& i : list) ostr << ' ' << i; return ostr; } int main () { std::list<int> list1{1, 2, 3, 4, 5}; std::list<int> list2{10, 20, 30, 40, 50}; auto it = list1.begin(); std::advance(it, 2); list1.splice(it, list2); std::cout << "list1:" << list1 << '\n'; std::cout << "list2:" << list2 << '\n'; list2.splice(list2.begin(), list1, it, list1.end()); std::cout << "list1:" << list1 << '\n'; std::cout << "list2:" << list2 << '\n'; }
输出
list1: 1 2 10 20 30 40 50 3 4 5 list2: list1: 1 2 10 20 30 40 50 list2: 3 4 5
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 250 | C++98 | 被移动元素的引用和迭代器 全部失效 |
它们引用或指向 *this 中的相同元素 |
N2525 | C++98 | 如果满足以下条件,则无法保证 O(1) 的拼接操作: get_allocator() != other.get_allocator(),则无法保证 O(1) 节点移动 |
在这种情况下,行为是 未定义的 |
[编辑] 另请参阅
归并两个已排序链表 (public member function) | |
移除满足特定标准的元素 (public member function) |