forward_list_merge

forward_list_merge (std::forward_list::merge — 병합)

std::forward_list 하나를 다른 forward_list로 병합하는 멤버 함수예요. 두 리스트는 정렬되어 있어야 하며, 병합 후에도 전체가 정렬 상태를 유지해요.

출처: cppreference

본문

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

void merge( forward_list& other );                 // (1) (since C++11)
void merge( forward_list&& other );                // (2) (since C++11)
template< class Compare >
void merge( forward_list& other, Compare comp );   // (3) (since C++11)
template< class Compare >
void merge( forward_list&& other, Compare comp );   // (4) (since C++11)

other*this와 같은 객체를 가리키면 아무것도 하지 않아요. 그렇지 않으면 other*this로 병합해요. 두 리스트 모두 정렬되어 있어야 해요. 원소는 복사되지 않고, 병합 후 컨테이너 other는 비어 있어요. 이 연산은 안정적(stable)이에요 — 두 리스트의 동등 원소에 대해 *this의 원소가 항상 other의 원소보다 앞에 오고, 두 리스트 각각의 동등 원소 순서도 변하지 않아요.

이터레이터나 참조는 무효화되지 않아요. *this에서 이동된 원소들을 가리키던 포인터·참조·이터레이터는 other가 아니라 *this의 같은 원소를 계속 가리켜요.

(1,2) 원소들을 std::less<T>() (C++14까지) / std::less<>() (C++14부터)로 비교해요.

(3,4) 원소들을 comp로 비교해요.

*thisother가 해당 비교자 기준으로 정렬되어 있지 않거나 할당자가 다르면 동작이 정의되지 않아요.

복잡도

std::distance(begin(), end()) + std::distance(other.begin(), other.end()) 비교 호출이 일어나요.

예제

#include <forward_list>
#include <iostream>
int main()
{
    std::forward_list<int> a{1, 3, 5};
    std::forward_list<int> b{2, 4, 6};
    a.merge(b);               // {1, 2, 3, 4, 5, 6}
    for (int x : a) std::cout << x << ' ';
}

더 알아보기 (Learn more)

cppreference