Namespaces
Variants

std::forward_list<T,Allocator>:: splice_after

From cppreference.net
void splice_after ( const_iterator pos, forward_list & other ) ;
(1) (自 C++11 起)
(自 C++26 起为 constexpr)
void splice_after ( const_iterator pos, forward_list && other ) ;
(2) (自 C++11 起)
(自 C++26 起为 constexpr)
void splice_after ( const_iterator pos, forward_list & other,
const_iterator it ) ;
(3) (自 C++11 起)
(自 C++26 起为 constexpr)
void splice_after ( const_iterator pos, forward_list && other,
const_iterator it ) ;
(4) (自 C++11 起)
(自 C++26 起为 constexpr)
void splice_after ( const_iterator pos, forward_list & other,
const_iterator first, const_iterator last ) ;
(5) (自 C++11 起)
(自 C++26 起为 constexpr)
void splice_after ( const_iterator pos, forward_list && other,
const_iterator first, const_iterator last ) ;
(6) (自 C++11 起)
(自 C++26 起为 constexpr)

将元素从 other 转移到 * this 。这些元素将被插入到 pos 之后。

若满足以下任一条件,则行为未定义:

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) 不抛出任何异常。

复杂度

1,2) other 的大小成线性关系。
3,4) 常量。
5,6) std:: distance ( first, last ) 呈线性关系。

示例

#include <cassert>
#include <forward_list>
int main()
{
    using F = std::forward_list<int>;
    // 演示重载(5)中开放区间(first, last)的含义:l1的第一个元素不会被转移
    F l1 = {1, 2, 3, 4, 5};
    F l2 = {10, 11, 12};
    l2.splice_after(l2.cbegin(), l1, l1.cbegin(), l1.cend());
    // 不等同于 l2.splice_after(l2.cbegin(), l1);
    // 后者等价于
    // 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}));
    // 重载(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{}));
    // 重载(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}));
    // 重载(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++标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 2045 C++11
get_allocator ( ) ! = other. get_allocator ( )
无法保证 O(1) 复杂度的拼接操作
此情况下
行为未定义
LWG 2222 C++11 it 指向的元素不会被转移,但指向它的
指针、引用和迭代器在拼接后会指向 * this 中的元素
仍指向
other 中的元素

参见

合并两个已排序的链表
(公开成员函数)
移除满足特定条件的元素
(公开成员函数)
返回指向起始元素前一个位置的迭代器
(公开成员函数)