std::list<T,Allocator>::splice
来自 cppreference.cn
void splice( const_iterator pos, list& other ); |
(1) | |
void splice( const_iterator pos, list&& other ); |
(2) | (since C++11) |
void splice( const_iterator pos, list& other, const_iterator it ); |
(3) | |
void splice( const_iterator pos, list&& other, const_iterator it ); |
(4) | (since 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) | (since C++11) |
将元素从一个列表转移到另一个列表。
没有元素被复制或移动,只有列表节点的内部指针被重新指向。没有迭代器或引用变为无效,移动元素的迭代器仍然有效,但现在指向 *this,而不是指向 other。
1,2) 将所有元素从 other 转移到 *this。 这些元素被插入到 pos 指向的元素之前。 操作后,容器 other 变为空。
3,4) 将 it 指向的元素从 other 转移到 *this。 该元素被插入到 pos 指向的元素之前。
5,6) 将范围
[
first,
last)
中的元素从 other 转移到 *this。 这些元素被插入到 pos 指向的元素之前。如果以下情况发生,则行为未定义
- get_allocator() != other.get_allocator(),
- 对于重载 (1,2),*this 和 other 指的是同一对象,
- 对于重载 (3,4),it 不是指向 other 的可解引用的迭代器,或者
- 对于重载 (5,6),
-
[
first,
last)
不是 other 中的有效范围,或者 - pos 在
[
first,
last)
中。
-
内容 |
[编辑] 参数
pos | - | 内容将被插入的元素之前 |
other | - | 从中转移内容的另一个容器 |
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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 250 | C++98 | 移动的元素的引用和迭代器 全部变为无效 |
它们引用或指向 *this 中的相同元素 |
N2525 | C++98 | 如果 O(1) 拼接无法保证 get_allocator() != other.get_allocator() |
行为是 在这种情况下行为未定义 |
[编辑] 参见
合并两个已排序的列表 (public member function) | |
移除满足特定标准的元素 (public member function) |