Share via


list::splice

ソース リストから要素を削除して、ターゲット リストに挿入します。

// insert the entire source list void splice( const_iterator Where, list<Type, Allocator>& Source ); void splice( const_iterator Where, list<Type, Allocator>&& Source );  // insert one element of the source list void splice( const_iterator Where, list<Type, Allocator>& Source, const_iterator Iter ); void splice( const_iterator Where, list<Type, Allocator>&& Source, const_iterator Iter );  // insert a range of elements from the source list void splice( const_iterator Where, list<Type, Allocator>& Source, const_iterator First, const_iterator Last );  void splice( const_iterator Where, list<Type, Allocator>&& Source, const_iterator First, const_iterator Last );

パラメーター

  • Where
    挿入されるターゲット リスト内の位置。

  • Source
    ターゲット リストに挿入されるソース リスト。

  • Iter
    ソース リストから挿入される要素。

  • First
    ソース リストから挿入される範囲内の最初の要素。

  • Last
    ソース リストから挿入される範囲内の最後の要素を超える最初の位置。

解説

メンバー関数の最初のペアは、ソース リスト内のすべての要素を、ターゲット リスト内の Where で参照される位置の前に挿入し、ソース リストからすべての要素を削除します (&Source を this と同じにすることはできません)。

メンバー関数の 2 つ目のペアは、Iter で参照される要素を、ターゲット リスト内の Where で参照される位置の前に挿入し、ソース リストから Iter を削除します (Where == Iter || Where == ++Iter の場合は、何も変わりません)。

メンバー関数の 3 つ目のペアは、[First, Last) で指定された範囲を、ターゲット リスト内の Where で参照される要素の前に挿入し、ソース リストからその要素の範囲を削除します (&Source == this の場合、範囲 [First, Last) に Where で指し示される要素を含めることはできません)。

範囲指定されたスプライスで N 個の要素が挿入され、さらに &Source != this の場合、クラス iterator のオブジェクトは N 回インクリメントされます。

すべての場合において、スプライスされた要素を参照する反復子、ポインター、参照は有効なままでターゲット コンテナーに転送されます。

使用例

// list_splice.cpp
// compile with: /EHsc /W4
#include <list>
#include <iostream>

using namespace std;

template <typename S> void print(const S& s) {
    cout << s.size() << " elements: ";

    for (const auto& p : s) {
        cout << "(" << p << ") ";
    }

    cout << endl;
}

int main()
{
    list<int> c1{10,11};
    list<int> c2{20,21,22};
    list<int> c3{30,31};
    list<int> c4{40,41,42,43};

    list<int>::iterator where_iter;
    list<int>::iterator first_iter;
    list<int>::iterator last_iter;

    cout << "Beginning state of lists:" << endl;
    cout << "c1 = ";
    print(c1);
    cout << "c2 = ";
    print(c2);
    cout << "c3 = ";
    print(c3);
    cout << "c4 = ";
    print(c4);

    where_iter = c2.begin();
    ++where_iter; // start at second element
    c2.splice(where_iter, c1);
    cout << "After splicing c1 into c2:" << endl;
    cout << "c1 = ";
    print(c1);
    cout << "c2 = ";
    print(c2);

    first_iter = c3.begin();
    c2.splice(where_iter, c3, first_iter);
    cout << "After splicing the first element of c3 into c2:" << endl;
    cout << "c3 = ";
    print(c3);
    cout << "c2 = ";
    print(c2);

    first_iter = c4.begin();
    last_iter = c4.end();
    // set up to get the middle elements
    ++first_iter;
    --last_iter;
    c2.splice(where_iter, c4, first_iter, last_iter);
    cout << "After splicing a range of c4 into c2:" << endl;
    cout << "c4 = ";
    print(c4);
    cout << "c2 = ";
    print(c2);
}
  

必要条件

ヘッダー: <list>

名前空間: std

参照

関連項目

list クラス

標準テンプレート ライブラリ