Determine elements of set1 that are not in set2 and elements of set2 that are not in set1 using set_symmetric_difference - C++ STL Algorithm

C++ examples for STL Algorithm:set_symmetric_difference

Description

Determine elements of set1 that are not in set2 and elements of set2 that are not in set1 using set_symmetric_difference

Demo Code

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

int main() //from  w ww.  j a v a 2 s  .c om
{ 
   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 symmetric_difference[ SIZE1 + SIZE2 ]; 

    // determine elements of a1 that are not in a2 and 
    // elements of a2 that are not in a1 
    int *ptr = set_symmetric_difference( a1, a1 + SIZE1, 
        a3, a3 + SIZE2, symmetric_difference ); 
    cout << "\n\nset_symmetric_difference of a1 and a3 is: "; 
    copy( symmetric_difference, ptr, output ); 

    cout << endl; 
}

Result


Related Tutorials