forward_list_splice_after

forward_list_splice_after (std::forward_list::splice_after — 원소 이어 붙이기)

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

출처: cppreference

본문

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

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

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

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

(3,4) other에서 it를 뒤따르는 원소 하나를 옮겨요.

(5,6) other에서 first를 뒤따르는 원소부터 last까지의 원소들을 옮겨요.

posfirstlast와 같은 위치를 가리키거나, firstlast가 서로 다른 리스트에 속해 있으면 동작이 정의되지 않아요.

복잡도

(1-4) 상수. (5,6) firstlast 사이 거리에 선형. other*this가 같은 객체면 상수.

예제

#include <forward_list>
#include <iostream>
int main()
{
    std::forward_list<int> a{1, 2, 3};
    std::forward_list<int> b{4, 5, 6};
    a.splice_after(a.begin(), b);   // a 뒤에 b 전체 이동
    for (int x : a) std::cout << x << ' ';  // 1 4 5 6 2 3
}

더 알아보기 (Learn more)

cppreference