set_union

set_union (합집합)

두 정렬 범위의 합집합을 출력하는 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

set_union은 정렬된 [first1, last1)[first2, last2)의 합집합을 정렬된 순서로 d_first에 복사해요.

template< class InputIt1, class InputIt2, class OutputIt >
OutputIt set_union( InputIt1 first1, InputIt1 last1,
                    InputIt2 first2, InputIt2 last2,
                    OutputIt d_first );   // (1)

비교기 버전도 있어요.

template< class InputIt1, class InputIt2, class OutputIt, class Compare >
OutputIt set_union( InputIt1 first1, InputIt1 last1,
                    InputIt2 first2, InputIt2 last2,
                    OutputIt d_first, Compare comp );   // (2)
  • 두 범위는 정렬되어 있어야 해요. 중복은 한 번만 포함돼요.
  • 반환 값: 마지막으로 쓴 출력 다음 반복자.
std::vector<int> a{1, 2, 3, 4};
std::vector<int> b{3, 4, 5, 6};
std::vector<int> out;
std::set_union(a.begin(), a.end(), b.begin(), b.end(),
               std::back_inserter(out));
// out == {1,2,3,4,5,6}

집합 이론의 "A ∪ B"를 정렬된 시퀀스로 구하는 함수예요.

더 알아보기 (Learn more)

cppreference