Determine elements that are in either or both sets using algorithm set_union. - C++ STL Algorithm

C++ examples for STL Algorithm:set_union

Description

Determine elements that are in either or both sets using algorithm set_union.

Demo Code

#include <iostream> 
#include <algorithm> // algorithm definitions 
#include <iterator> // ostream_iterator 
using namespace std; 

int main() /*w w  w  .j a  v a2s .c  o  m*/
{ 
   const int SIZE1 = 10, SIZE2 = 5, SIZE3 = 20; 
   int a1[ SIZE1 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
   int a2[ SIZE2 ] = { 4, 5, 6, 7, 8 }; 
   int a3[ SIZE2 ] = { 4, 5, 6, 11, 15 }; 
   ostream_iterator< int > output( cout, " " ); 

   cout << "a1 contains: "; 
   copy( a1, a1 + SIZE1, output ); // display array a1 
   cout << "\na2 contains: "; 
   copy( a2, a2 + SIZE2, output ); // display array a2 
   cout << "\na3 contains: "; 
   copy( a3, a3 + SIZE2, output ); // display array a3 

    int unionSet[ SIZE3 ]; 

    // determine elements that are in either or both sets 
    int *ptr = set_union( a1, a1 + SIZE1, a3, a3 + SIZE2, unionSet ); 
    cout << "\n\nset_union of a1 and a3 is: "; 
    copy( unionSet, ptr, output ); 
    cout << endl; 
}

Result


Related Tutorials