名前空間
変種
操作

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

From cppreference.com
 
 
 
 
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が指す要素の後ろに挿入されます。

要素のコピーは行われません。イテレータや参照は無効になりません。移動された要素へのイテレータは、otherではなく、*thisを参照するようになります。

1,2) otherの全ての要素を*thisに移動させます。操作後、コンテナotherは空になります。
3,4) otherから、itが指す要素の次の要素を*thisに移動させます。pos == itまたはpos == ++itの場合、効果はありません。
5,6) otherから、範囲(firstlast)の要素を*thisに移動させます。firstが指す要素は移動されません。

以下の場合、動作は未定義です:

  • O(1) ノード移動を保証できませんでした,
  • posbefore_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++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2045 C++11 O(1)スプライスが保証されない場合
O(1) ノード移動を保証できませんでした
この場合の動作は
未定義となる
LWG 2222 C++11 itが指す要素は移動されないが、それへのポインタ、参照、および
イテレータがスプライス後に*this内の要素を参照するようになる場合
依然として
other内の要素を参照してしまう

[編集] 関連項目

ソート済みの2つのリストをマージする
(公開メンバ関数) [編集]
特定の基準を満たす要素を削除する
(public member function) [編集]
先頭の前を指すイテレータを返す
(公開メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)