Determine elements in one set but not in another set : set_difference « STL Algorithms Merge « C++






Determine elements in one set but not in another set

 
 

#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <iterator>

int main()
{
   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, 7, 6, 11, 15 };
   std::ostream_iterator< int > output( cout, " " );

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

   int difference[ SIZE1 ];

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

   return 0;
}

/* 
a1 contains: 1 2 3 4 5 6 7 8 9 10
a2 contains: 4 5 6 7 8
a3 contains: 4 7 6 11 15

set_difference of a1 and a2 is: 1 2 3 9 10 
 */        
  








Related examples in the same category

1.Determine elements of first range without elements of second range by using set_difference()