The union of two sets is formed by the elements that are present in either one of the sets, or in both.
The comparison to check for equivalence of values, uses either operator< for the first version, or comp for the second, in order to test this; The value of an element, a, is equivalent to another one, b, when (!a<b && !b<a) or (!comp(a,b) && !comp(b,a)).
For the function to yield the expected result, the elements in the ranges shall be already ordered according to the same strict weak ordering criterion (operator< or comp).
The behavior of this function template is equivalent to:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Parameters
- first1, last1
- Input iterators to the initial and final positions of the first sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
- first2, last2
- Input iterators to the initial and final positions of the second sequence. The range used is [first2,last2).
- comp
- Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
Return value
An iterator to the end of the constructed range.Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Output:
union has 8 elements |
Complexity
At most, performs 2*(count1+count2)-1 comparisons or applications of comp (where countX is the distance between firstX and lastX).See also
| set_intersection | Intersection of two sorted ranges (function template) |
| set_difference | Difference of two sorted ranges (function template) |
| set_symmetric_difference | Symmetric difference of two sorted ranges (function template) |
| merge | Merge sorted ranges (function template) |
