C++ examples for STL Algorithm:set_intersection
Determine elements in both set with algorithm set_intersection
#include <iostream> #include <algorithm> // algorithm definitions #include <iterator> // ostream_iterator using namespace std; int main() /*from w w w .j a v a2s.co 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 intersection[ SIZE1 ]; // determine elements in both a1 and a2 int *ptr = set_intersection( a1, a1 + SIZE1, a2, a2 + SIZE2, intersection ); cout << "\n\nset_intersection of a1 and a2 is: "; copy( intersection, ptr, output ); }