1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
template<typename C> C get_intersection( C &a, C &b )
{
sort( a.begin(), a.end() );
sort( b.begin(), b.end() );
C result;
set_intersection( a.begin(), a.end(), b.begin(), b.end(), back_inserter( result ) );
return result;
}
template<typename C> void print( C c )
{
for ( auto e : c ) cout << e << ' ';
cout << '\n';
}
int main()
{
vector<int> v1 = { 1, 45, 54, 71, 76, 12 };
vector<int> v2 = { 1, 7, 5, 4, 6, 12 };
print( get_intersection( v1, v2 ) );
vector<char> c1 = { 'z', 'Z', 'h', 'd', 'I', 'A', 'Z', 'A' };
vector<char> c2 = { 'Z', 'A', 'm', 'M', 'i', 'D', 'h' };
print( get_intersection( c1, c2 ) );
string s1 = "zZhdIAZA";
string s2 = "ZAmMiDh";
print( get_intersection( s1, s2 ) );
}
|