다음을 통해 공유


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와 동일할 수 없습니다.

멤버 함수의 두 번째 쌍은 Iter에서 참조하는 요소를 Where에서 참조하는 대상 목록의 위치 앞에 삽입하고 원본 목록에서 Iter을 제거합니다. Where == Iter || Where == ++Iter인 경우 아무 것도 변경되지 않습니다.

멤버 함수의 세 번째 쌍은 [First, Last)로 지정된 범위를 Where에서 참조하는 대상 목록의 요소 앞에 삽입하고 원본 목록에서 해당 요소 범위를 제거합니다. &Source == this인 경우 [First, Last) 범위는 Where가 가리키는 요소를 제외해야 합니다.

범위가 지정된 스플라이스가 N개 요소 및 &Source != this를 삽입하면 클래스 반복기의 개체가 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 클래스

표준 템플릿 라이브러리