Standard C++ Library Class Reference

sequences, only the element from the first sequence is copied to result. (Use the merge algorithm to create an
ordered merge of two sorted sequences that contains all the elements from both sequences.)
set_union assumes that the sequences are sorted using the default comparision operator less than (<), unless an
alternative comparison operator (comp) is provided.
Complexity
At most ((last1 - first1) + (last2 - first2)) * 2 -1 comparisons are performed.
Example
//
// set_unin.cpp
//
#include <algorithm>
#include <set>
#include <iostream.h>
int main()
{
//Initialize some sets
int a2[6] = {2,4,6,8,10,12};
int a3[4] = {3,5,7,8};
set<int, less<int> > even(a2, a2+6),
result, small(a3,a3+4);
//Create an insert_iterator for result
insert_iterator<set<int, less<int> > >
res_ins(result, result.begin());
//Demonstrate set_union
cout << "The result of:" << endl << "{";
copy(small.begin(),small.end(),
ostream_iterator<int>(cout," "));
cout << "} union {";
copy(even.begin(),even.end(),
ostream_iterator<int>(cout," "));
cout << "} =" << endl << "{";
set_union(small.begin(), small.end(),
even.begin(), even.end(), res_ins);
copy(result.begin(),result.end(),
ostream_iterator<int>(cout," "));
cout << "}" << endl << endl;
return 0;
}
Output :
The result of:
{3 5 7 8 } union {2 4 6 8 10 12 } =
{2 3 4 5 6 7 8 10 12 }
Warning
If your compiler does not support default template parameters, then you need to always supply the Compare
template argument and the Allocator template argument. For instance, you will need to write :