命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | 容器‎ | forward_list
 
 
 
 
void splice_after( const_iterator pos, forward_list& other );
(1) (C++11 起)
void splice_after( const_iterator pos, forward_list&& other );
(2) (C++11 起)
void splice_after( const_iterator pos, forward_list& other,
                   const_iterator it );
(3) (C++11 起)
void splice_after( const_iterator pos, forward_list&& other,
                   const_iterator it );
(4) (C++11 起)
void splice_after( const_iterator pos, forward_list& other,
                   const_iterator first, const_iterator last );
(5) (C++11 起)
void splice_after( const_iterator pos, forward_list&& other,
                   const_iterator first, const_iterator last );
(6) (C++11 起)

将元素从另一个 forward_list 移动到 *this。元素插入到 pos 指向的元素之后。

不复制任何元素。没有迭代器或引用失效。移动的元素现在引用 *this,而不是 other

1,2) 将所有元素从 other 移动到 *this。操作后容器 other 变为空。
3,4)it 后面的迭代器指向的元素从 other 移动到 *this。如果 pos == itpos == ++it,则无效。
5,6) 将范围 (firstlast) 中的元素从 other 移动到 *this。不移动 first 指向的元素。

若出现以下情况,行为未定义:

  • get_allocator() != other.get_allocator(),则无法保证 O(1) 节点移动,
  • pos 既不是 before_begin() 也不是 [begin()end())可解引用迭代器
  • 对于重载 (1,2)*thisother 指的是同一个对象,
  • 对于重载 (3,4)it 后面的迭代器不是 other可解引用迭代器,或
  • 对于重载 (5,6)
  • (firstlast) 不是 other 中的有效范围
  • (firstlast) 中的某些迭代器不可解引用,或
  • pos 位于 (firstlast) 中。

目录

[编辑] 参数

pos - 插入内容在其后的元素
其他 - 另一个容器,从中移动内容
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++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 2045 C++11 如果满足以下条件,则不能保证 O(1) 拼接
get_allocator() != other.get_allocator(),则无法保证 O(1) 节点移动
在这种情况下,行为是
未定义的
LWG 2222 C++11 it 指向的元素未移动,但指向它的指针、引用和
拼接后引用它的迭代器将引用 *this 中的一个元素
仍引用
other 中的元素

[编辑] 另请参阅

归并两个已排序链表
(public member function) [编辑]
移除满足特定标准的元素
(public member function) [编辑]
返回指向起始元素之前的元素的迭代器
(public member function) [编辑]