std::forward_list<T,Allocator>::splice_after
来自 cppreference.cn
< cpp | 容器 | forward_list
void splice_after( const_iterator pos, forward_list& other ); |
(1) | (since C++11) |
void splice_after( const_iterator pos, forward_list&& other ); |
(2) | (since C++11) |
void splice_after( const_iterator pos, forward_list& other, const_iterator it ); |
(3) | (since C++11) |
void splice_after( const_iterator pos, forward_list&& other, const_iterator it ); |
(4) | (since C++11) |
void splice_after( const_iterator pos, forward_list& other, const_iterator first, const_iterator last ); |
(5) | (since C++11) |
void splice_after( const_iterator pos, forward_list&& other, const_iterator first, const_iterator last ); |
(6) | (since C++11) |
将元素从另一个 forward_list
移动到 *this。元素被插入到 pos 指向的元素之后。
没有元素被复制。没有迭代器或引用会失效。移动后元素的迭代器现在指向 *this,而不是 other。
1,2) 将所有元素从 other 移动到 *this。操作后,容器 other 将变为空。
3,4) 将迭代器 it 之后的元素从 other 移动到 *this。如果 pos == it 或 pos == ++it,则无效。
5,6) 将范围
(
first,
last)
中的元素从 other 移动到 *this。first 指向的元素不会被移动。在以下情况下,行为是未定义的:
- get_allocator() != other.get_allocator(),
- pos 既不是 before_begin() 也不是 可解引用的迭代器,范围为
[
begin(),
end())
, - 对于重载 (1,2),*this 和 other 指的是同一对象,
- 对于重载 (3,4),it 之后的迭代器不是指向 other 的 可解引用的迭代器,或者
- 对于重载 (5,6),
目录 |
[编辑] 参数
pos | - | 内容将被插入到此元素之后 |
other | - | 从中移动内容的另一个容器 |
it | - | 在要从 other 移动到 *this 的元素迭代器之前的迭代器 |
first, last | - | 定义要从 other 移动到 *this 的元素范围的迭代器对 |
[编辑] 异常
不抛出任何异常。
[编辑] 复杂度
1,2) 与 other 的大小呈线性关系。
3,4) 常数。
5,6) 与 std::distance(first, last) 呈线性关系。
[编辑] 示例
运行此代码
#include <cassert> #include <forward_list> int main() { using F = std::forward_list<int>; // Demonstrate the meaning of open range (first, last) // in overload (5): the first element of l1 is not moved. F l1 = {1, 2, 3, 4, 5}; F l2 = {10, 11, 12}; l2.splice_after(l2.cbegin(), l1, l1.cbegin(), l1.cend()); // Not equivalent to l2.splice_after(l2.cbegin(), l1); // which is equivalent to // l2.splice_after(l2.cbegin(), l1, l1.cbefore_begin(), l1.end()); assert((l1 == F{1})); assert((l2 == F{10, 2, 3, 4, 5, 11, 12})); // Overload (1) F x = {1, 2, 3, 4, 5}; F y = {10, 11, 12}; x.splice_after(x.cbegin(), y); assert((x == F{1, 10, 11, 12, 2, 3, 4, 5})); assert((y == F{})); // Overload (3) x = {1, 2, 3, 4, 5}; y = {10, 11, 12}; x.splice_after(x.cbegin(), y, y.cbegin()); assert((x == F{1, 11, 2, 3, 4, 5})); assert((y == F{10, 12})); // Overload (5) x = {1, 2, 3, 4, 5}; y = {10, 11, 12}; x.splice_after(x.cbegin(), y, y.cbegin(), y.cend()); assert((x == F{1, 11, 12, 2, 3, 4, 5})); assert((y == F{10})); }
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2045 | C++11 | 如果 O(1) 拼接无法保证 get_allocator() != other.get_allocator() |
在这种情况下,行为是 未定义的 |
LWG 2222 | C++11 | 由 it 指向的元素不会被移动,但指针、引用和 引用它的迭代器在拼接后会引用 *this 中的元素 |
仍然引用 other 中的元素 |
[编辑] 参见
合并两个已排序的列表 (公共成员函数) | |
移除满足特定条件的元素 (公共成员函数) | |
返回指向起始元素之前的元素的迭代器 (公共成员函数) |