list_splice

list_splice (std::list::splice — 원소 이어 붙이기)

std::list에서 다른 리스트의 원소들을 특정 위치로 이동시키는 멤버 함수예요. 원소는 복사되지 않고 노드가 통째로 이전돼요.

출처: cppreference

본문

시그니처는 다음과 같아요.

void splice( const_iterator pos, list& other );                // (1)
void splice( const_iterator pos, list&& other );               // (2) (since C++11)
void splice( const_iterator pos, list& other, const_iterator it );   // (3)
void splice( const_iterator pos, list&& other, const_iterator it );  // (4) (since C++11)
void splice( const_iterator pos, list& other,
             const_iterator first, const_iterator last );      // (5)
void splice( const_iterator pos, list&& other,
             const_iterator first, const_iterator last );      // (6) (since C++11)

다른 list에서 원소를 이동해서 pos 앞에 이어 붙여요. 원소는 복사·이동되지 않고 노드 연결이 바뀌므로 상수 시간(또는 범위에 비례)에 수행돼요. other의 원소가 *this로 옮겨져요.

(1,2) other의 모든 원소를 옮겨요.

(3,4) otherit 앞의 원소 하나를 옮겨요. it*thisother의 원소를 가리킬 수 있지만, posit를 가리키면 동작이 정의되지 않아요.

(5,6) other[first, last) 범위의 원소들을 옮겨요. pos[first, last) 안에 있으면 동작이 정의되지 않아요.

복잡도

(1-4) 상수. (5,6) std::distance(first, last)에 선형.

예제

#include <list>
#include <iostream>
int main()
{
    std::list<int> a{1, 2, 3};
    std::list<int> b{4, 5, 6};
    auto it = a.begin();
    ++it;                       // 2를 가리킴
    a.splice(it, b);            // b 전체를 a의 2 앞에 이동
    for (int x : a) std::cout << x << ' ';  // 1 4 5 6 2 3
}

더 알아보기 (Learn more)

cppreference