|
set_union
function template
<algorithm>
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result );
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp );
Union of two sorted ranges
Constructs a sorted range beginning in the location pointed by result with the set union of the two sorted ranges [first1,last1) and [first2,last2) as content.
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
|
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result )
{
while (true)
{
if (*first1<*first2) *result++ = *first1++;
else if (*first2<*first1) *result++ = *first2++;
else { *result++ = *first1++; first2++; }
if (first1==last1) return copy(first2,last2,result);
if (first2==last2) return copy(first1,last1,result);
}
}
|
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
|
// set_union example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
vector<int>::iterator it;
sort (first,first+5); // 5 10 15 20 25
sort (second,second+5); // 10 20 30 40 50
it=set_union (first, first+5, second, second+5, v.begin());
// 5 10 15 20 25 30 40 50 0 0
cout << "union has " << int(it - v.begin()) << " elements.\n";
return 0;
}
|
Output:
Complexity
At most, performs 2*(count1+count2)-1 comparisons or applications of comp (where countX is the distance between firstX and lastX).
See also
merge | Merge sorted ranges (function template) |
|