名稱空間
變體
操作

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

來自 cppreference.com
< 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) [編輯]