Determine elements in one set but not another set with algorithm set_difference - C++ STL Algorithm

C++ examples for STL Algorithm:set_difference

Description

Determine elements in one set but not another set with algorithm set_difference

Demo Code

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

int main() //  w w w  .  ja v  a 2s  .  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 difference[ SIZE1 ]; 

   // determine elements of a1 not in a2 
   int *ptr = set_difference( a1, a1 + SIZE1, a2, a2 + SIZE2, difference ); 
   cout << "\n\nset_difference of a1 and a2 is: "; 
   copy( difference, ptr, output ); 

}

Result


Related Tutorials