std::list<T,Allocator>:: splice
From cppreference.net
|
void
splice
(
const_iterator pos, list
&
other
)
;
|
(1) | (自 C++26 起为 constexpr) |
|
void
splice
(
const_iterator pos, list
&&
other
)
;
|
(2) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
|
void
splice
(
const_iterator pos, list
&
other, const_iterator it
)
;
|
(3) | (自 C++26 起为 constexpr) |
|
void
splice
(
const_iterator pos, list
&&
other, const_iterator it
)
;
|
(4) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
|
void
splice
(
const_iterator pos, list
&
other,
const_iterator first, const_iterator last ) ; |
(5) | (自 C++26 起为 constexpr) |
|
void
splice
(
const_iterator pos, list
&&
other,
const_iterator first, const_iterator last ) ; |
(6) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
将元素从 other 转移到 * this 。这些元素将被插入到 pos 位置。
若满足以下任一条件,则行为未定义:
-
pos
不在范围
[begin ( ),end ( ))内。 - get_allocator ( ) == other. get_allocator ( ) 为 false 。
1,2)
转移
other
的所有元素。该操作完成后
other
变为空。
如果
*
this
与
other
指向同一对象,则行为未定义。
3,4)
转移由
it
指向的元素。
*
this
与
other
可能指向同一对象。在此情况下,若
pos
==
it
或
pos
==
++
it
为
true
则无任何效果。
如果
it
不在范围
[
begin
(
)
,
end
(
)
)
内,则行为未定义。
5,6)
传输范围
[
first
,
last
)
内的元素。
*
this
与
other
可能指向同一对象。
若满足以下任意条件,则行为未定义:
-
[first,last)在 other 中不是 有效范围 , -
[first,last)中的任何迭代器不可解引用。 -
pos
位于
[first,last)区间内。
不会使任何迭代器或引用失效。如果 * this 与 other 指向不同对象,则被转移元素的迭代器现在将指向 * this ,而非 other 。
目录 |
参数
| 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++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 250 | C++98 |
对被移动元素的引用和迭代器
均会失效 |
它们引用或指向
* this 中的相同元素 |
| N2525 | C++98 |
当
get_allocator ( ) ! = other. get_allocator ( ) 时 无法保证O(1)复杂度的拼接操作 |
此情况下
行为未定义 |
参见
|
合并两个已排序的列表
(公开成员函数) |
|
|
移除满足特定条件的元素
(公开成员函数) |