std::set_union
| 헤더 <algorithm>
|
||
template< class InputIt1, class InputIt2, class OutputIt >
OutputIt set_union( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first );
|
에 정의됨 (1) | (C++20부터 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class ForwardIt3 >
ForwardIt3 set_union( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first );
|
(2) | (C++17부터) |
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 );
|
(3) | (C++20부터 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2,
class ForwardIt3, class Compare >
ForwardIt3 set_union( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first, Compare comp );
|
(4) | (C++17부터) |
d_first에서 시작하는 정렬된 합집합을 구성합니다. 이는 하나 또는 두 정렬된 범위 [first1, last1)와 [first2, last2)에 존재하는 요소들의 집합으로 구성됩니다.
[first1, last1)에 서로 동등한 m개의 요소가 있고 [first2, last2)에 그들과 동등한 n개의 요소가 있으면, 모든 m개의 요소가 [first1, last1)에서 출력 범위로 순서를 유지하며 복사되고, 그 다음 마지막 std::max(n - m, 0)개의 요소가 [first2, last2)에서 출력 범위로 역시 순서를 유지하며 복사됩니다.
[first1, last1) 또는 [first2, last2)이 정렬되지 않은 경우 (operator<에 대해 (C++20까지)std::less{}(C++20부터)), 동작은 정의되지 않습니다.[first1, last1) 또는 [first2, last2)이 comp에 대해 정렬되지 않은 경우, 동작은 정의되지 않습니다.policy에 따라 실행됩니다.true인 경우에만 오버로드 해결에 참여합니다:
|
|
(C++20까지) |
|
|
(C++20부터) |
출력 범위가 [first1, last1) 또는 [first2, last2)와 겹치는 경우, 동작은 정의되지 않습니다.
매개변수
| first1, last1 | - | 첫 번째 입력 정렬된 범위 의 요소들을 정의하는 반복자 쌍 |
| first2, last2 | - | 두 번째 입력 정렬된 범위 의 요소들을 정의하는 반복자 쌍 |
| d_first | - | 출력 범위의 시작 |
| policy | - | 사용할 실행 정책 |
| comp | - |
비교 함수 객체(즉,
Compare
요구 사항을 만족하는 객체)로, 첫 번째 인수가 두 번째 인수보다
작은 경우
(즉,
먼저 정렬되는 경우
)
true
를 반환합니다.
비교 함수의 시그니처는 다음과 동일해야 합니다: bool cmp ( const Type1 & a, const Type2 & b ) ;
시그니처에
const
&
가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며
값 범주
에 관계없이
|
| 타입 요구 사항 | ||
-
InputIt1, InputIt2
는
LegacyInputIterator
요구 사항을 충족해야 합니다.
|
||
-
ForwardIt1, ForwardIt2, ForwardIt3
는
LegacyForwardIterator
요구 사항을 충족해야 합니다.
|
||
-
OutputIt
는
LegacyOutputIterator
요구 사항을 충족해야 합니다.
|
||
-
Compare
는
Compare
요구 사항을 충족해야 합니다.
|
||
반환값
구성된 범위의 끝을 지난 반복자.
복잡도
N 1 를 std:: distance ( first1, last1 ) 로, N 2 를 std:: distance ( first2, last2 ) 로 정의할 때:
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패할 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
| set_union (1) |
|---|
template<class InputIt1, class InputIt2, class OutputIt> OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first) { for (; first1 != last1; ++d_first) { if (first2 == last2) return std::copy(first1, last1, d_first); if (*first2 < *first1) *d_first = *first2++; else { *d_first = *first1; if (!(*first1 < *first2)) ++first2; ++first1; } } return std::copy(first2, last2, d_first); } |
| set_union (3) |
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) { for (; first1 != last1; ++d_first) { if (first2 == last2) // 범위 2 완료, 범위 1의 나머지 요소 포함: return std::copy(first1, last1, d_first); if (comp(*first2, *first1)) *d_first = *first2++; else { *d_first = *first1; if (!comp(*first1, *first2)) // 동등함 => *first2를 포함할 필요 없음 ++first2; ++first1; } } // 범위 1 완료, 범위 2의 나머지 요소 포함: return std::copy(first2, last2, d_first); } |
참고 사항
이 알고리즘은
std::merge
가 수행하는 작업과 유사한 작업을 수행합니다. 둘 다 정렬된 두 입력 범위를 소비하고 두 입력의 요소들로 구성된 정렬된 출력을 생성합니다. 이 두 알고리즘의 차이는 동등하게 비교되는 양쪽 입력 범위의 값들을 처리하는 방식에 있습니다 (참고:
LessThanComparable
). 만약 동등한 값들이 첫 번째 범위에서
n
번 나타나고 두 번째 범위에서
m
번 나타난다면,
std::merge
는 모든
n
+
m
개의 발생을 출력하는 반면,
std::set_union
은
std::
max
(
n, m
)
개만 출력합니다. 따라서
std::merge
는 정확히
std::
distance
(
first1, last1
)
+
std::
distance
(
first2, last2
)
개의 값을 출력하는 반면,
std::set_union
은 더 적은 수를 생성할 수 있습니다.
예제
#include <algorithm> #include <iostream> #include <iterator> #include <vector> void println(const std::vector<int>& v) { for (int i : v) std::cout << i << ' '; std::cout << '\n'; } int main() { std::vector<int> v1, v2, dest; v1 = {1, 2, 3, 4, 5}; v2 = {3, 4, 5, 6, 7}; std::set_union(v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), std::back_inserter(dest)); println(dest); dest.clear(); v1 = {1, 2, 3, 4, 5, 5, 5}; v2 = {3, 4, 5, 6, 7}; std::set_union(v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), std::back_inserter(dest)); println(dest); }
출력:
1 2 3 4 5 6 7 1 2 3 4 5 5 5 6 7
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 291 | C++98 | 입력 범위에서 동등한 요소를 어떻게 처리할지 명시되지 않았음 | 명시됨 |
같이 보기
| 한 시퀀스가 다른 시퀀스의 부분 시퀀스인지 결정합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 두 개의 정렬된 범위를 병합합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 두 집합 간의 차집합을 계산합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 두 집합의 교집합을 계산합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
| 두 집합의 대칭 차집합을 계산합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
두 집합의 합집합을 계산합니다 (알고리즘 함수 객체) |