set_union

Syntax:

    #include <algorithm>
    output_iterator set_union( input_iterator start1, input_iterator end1, input_iterator2 start2, input_iterator2 end2, output_iterator result );
    output_iterator set_union( input_iterator start1, input_iterator end1, input_iterator2 start2, input_iterator2 end2, output_iterator result, StrictWeakOrdering cmp );

The set_union() algorithm computes the sorted union of the two sorted ranges [start1,end1) and [start2,end2) and stores it starting at result.

The return value of set_union() is an iterator to the end of the union range.

set_union() runs in linear time. Example

// 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: union has 8 elements

Related Topics: includes, merge, set_difference, set_intersection, set_symmetric_difference