Namespaces
Variants

std::list<T,Allocator>:: splice

From cppreference.net
void splice ( const_iterator pos, list & other ) ;
(1) (constexpr since C++26)
void splice ( const_iterator pos, list && other ) ;
(2) (since C++11)
(constexpr since C++26)
void splice ( const_iterator pos, list & other, const_iterator it ) ;
(3) (constexpr since C++26)
void splice ( const_iterator pos, list && other, const_iterator it ) ;
(4) (since C++11)
(constexpr since C++26)
void splice ( const_iterator pos, list & other,
const_iterator first, const_iterator last ) ;
(5) (constexpr since C++26)
void splice ( const_iterator pos, list && other,
const_iterator first, const_iterator last ) ;
(6) (since C++11)
(constexpr since C++26)

other 에서 * this 로 요소들을 전송합니다. 요소들은 pos 위치에 삽입됩니다.

다음 조건 중 하나라도 충족되면, 동작은 정의되지 않습니다:

  • pos 가 범위 [ begin ( ) , end ( ) ) 내에 있지 않습니다.
  • get_allocator ( ) == other. get_allocator ( ) false 입니다.
1,2) other 의 모든 요소를 전송합니다. other 는 연산 후 비어 있는 상태가 됩니다.
만약 * this other 가 동일한 객체를 참조할 경우, 동작은 정의되지 않습니다.
3,4) it 가 가리키는 요소를 전송합니다.
* this other 가 동일한 객체를 참조할 수 있습니다. 이 경우 pos == it 또는 pos == ++ it true 인 경우 아무런 효과가 없습니다.
만약 it 이 범위 [ begin ( ) , end ( ) ) 내에 있지 않으면, 동작은 정의되지 않습니다.
5,6) [ first , last ) 범위의 요소들을 전송합니다.
* this other 는 동일한 객체를 참조할 수 있습니다.
다음 조건 중 하나라도 만족되면, 동작은 정의되지 않음:
  • [ first , last ) 유효한 범위 가 아닌 경우 other 에서,
  • [ first , last ) 내의 임의의 반복자가 역참조 가능하지 않은 경우.
  • pos [ first , last ) 에 포함되는 경우.

어떤 반복자나 참조도 무효화되지 않습니다. 만약 * this other 가 서로 다른 객체를 참조하는 경우, 전달된 요소들에 대한 반복자들은 이제 * this 를 가리키며, other 를 가리키지 않습니다.

목차

매개변수

pos - 콘텐츠가 삽입될 요소 앞의 위치
other - 콘텐츠를 전송할 다른 컨테이너
it - other 에서 * this 로 전송할 요소
first, last - other 에서 * this 로 전송할 요소들의 범위 를 정의하는 반복자 쌍

복잡도

1-4) 상수.
5,6) 만약 other * this 와 동일한 객체를 참조하면 상수 시간, 그렇지 않으면 std:: distance ( first, last ) 에 선형인 시간 복잡도를 가집니다.

예제

#include <iostream>
#include <list>
std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list)
{
    for (auto& i : list)
        ostr << ' ' << i;
    return ostr;
}
int main ()
{
    std::list<int> list1{1, 2, 3, 4, 5};
    std::list<int> list2{10, 20, 30, 40, 50};
    auto it = list1.begin();
    std::advance(it, 2);
    list1.splice(it, list2);
    std::cout << "list1:" << list1 << '\n';
    std::cout << "list2:" << list2 << '\n';
    list2.splice(list2.begin(), list1, it, list1.end());
    std::cout << "list1:" << list1 << '\n';
    std::cout << "list2:" << list2 << '\n';
}

출력:

list1: 1 2 10 20 30 40 50 3 4 5
list2:
list1: 1 2 10 20 30 40 50
list2: 3 4 5

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 250 C++98 이동된 요소에 대한 참조와 반복자가
모두 무효화됨
해당 참조나 포인터가
* this 내 동일한 요소를 가리킴
N2525 C++98 get_allocator ( ) ! = other. get_allocator ( ) 인 경우
O(1) 스플라이싱을 보장할 수 없음
이 경우 동작은
정의되지 않음

참고 항목

두 개의 정렬된 리스트를 병합
(public member function)
특정 조건을 만족하는 요소들을 제거
(public member function)