set_union function template — Computes union of sorted ranges
template<typename InIter1, typename InIter2, typename OutIter> OutIter set_union(InIter1 first1, InIter1 last1, InIter2 first2, InIter2 last2, OutIter result); template<typename InIter1, typename InIter2, typename OutIter, typename Compare> OutIter set_union(InIter1 first1, InIter1 last1, InIter2 first2, InIter2 last2, OutIter result, Compare comp);
The set_union
function
template merges elements from the sorted ranges [first1
, last1
) and [first2
, last2
), copying the sorted, merged results
to the range starting at result
.
If an element is present in both ranges, only one element is copied
to the result range. If the input ranges contain duplicates, each
occurrence of an element in an input range results in a copy in the
result range. An iterator that points to one past the end of the
result range is returned.
The result range must not overlap either source range.
The first version compares items using the <
operator. The second version uses
comp(X
, Y)
to test whether X
<
Y
.
Figure 13-20 shows an example of a set union using multisets.
Precondition: !(*(i
+ 1)
< *i
) for all i
in [first1
, last1
- 1) and !(*(j
+ 1) < *j
) for all
j
in [first2
,
last2
- 1).
Postcondition: !(*(i
+ 1)
< *i
) for all i
in [result
, return
- 1).
Complexity is linear: at most 2 × ((last1
- first1
) + (last2
- first2
)) - 1 comparisons are
performed.