set_intersection and back_inserter : union intersection « set multiset « C++ Tutorial






#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}

int main( )
{
   const int num_years = 5;
   const char* d[num_years] = { "A", "B","C", "D", "E" };
   const char* d2[num_years] = { "a", "b","c", "d", "e" };
   const char* d3[num_years] = { "1","2", "3",  "4", "5" };

   multiset<string> s1( d,d+num_years );
   multiset<string> s2( d2,d2+num_years );
   multiset<string> s3( d3,d3+num_years );

   print( s1 );
   print( s2 );
   print( s3 );

   vector<string> winner;
   set_intersection( s1.begin(), s1.end(), s2.begin(), s2.end(), back_inserter( winner ) );
   if( !winner.empty() )
   {
      print( winner );
   }
   else
      cout << "No horse won both s1 and s2\n";

}








19.19.union intersection
19.19.1.set_intersection and back_inserter
19.19.2.set_symmetric_difference and back_inserter
19.19.3.set_difference and back_inserter