Namespaces
Variants

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

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

other から要素を * this に転送します。要素は pos の後に挿入されます。

以下のいずれかの条件が満たされる場合、動作は未定義です:

  • pos が範囲 ( before_begin() , end() ) 内にありません。
  • get_allocator ( ) == other. get_allocator ( ) false です。
1,2) other の全要素を転送します。 この操作後、 other は空になります。
*thisと * this および other が同じオブジェクトを参照する場合、動作は未定義です。
3,4) 次の要素を転送します it
* this other は同じオブジェクトを参照する可能性があります。この場合、 pos == it または pos == ++ it true の場合、効果はありません。
++ itが範囲 [ begin ( ) , end ( ) ) 内にない場合、動作は未定義です。
5,6) 範囲 ( first , last ) 内の要素を転送します。
* this other は同じオブジェクトを参照する可能性があります。
以下のいずれかの条件が満たされる場合、動作は未定義です:
  • ( first , last ) 有効な範囲 ではない場合、
  • ( first , last ) 内のいずれかのイテレータが間接参照可能でない場合、
  • pos ( first , last ) 内にある場合。

イテレータや参照は無効化されない。 *this other が異なるオブジェクトを参照する場合、転送された要素へのイテレータは other ではなく *this を参照するようになる。

目次

パラメータ

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++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2045 C++11 O(1) splicing could not be guaranteed if
get_allocator ( ) ! = other. get_allocator ( )
この場合の動作は
未定義
LWG 2222 C++11 the element pointed to by it is not transferred, but pointers, references and
iterators referring to it would refer to an element in * this after splicing
依然として
other 内の要素を参照

関連項目

2つのソート済みリストをマージする
(公開メンバ関数)
特定の条件を満たす要素を削除する
(公開メンバ関数)
先頭要素の前を指すイテレータを返す
(公開メンバ関数)