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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <algorithm>
#include <string>
#include <set>
#include <iostream>
#include <iterator>
using namespace std;
struct str_pair
{
str_pair( const string & first, const string & second ) : first_( first ), second_( second ) {}
bool operator < ( const str_pair & other ) const { return ( first_ < other.first_ ); }
string first_, second_;
};
ostream& operator << ( ostream & ostr, const str_pair & pair_in )
{
ostr << pair_in.first_ << "/" << pair_in.second_;
return ostr;
}
bool MyComp( const string & left, const str_pair & right )
{
return ( left == right.first_ );
}
int main(int argc, char* argv[])
{
set<string> v1;
set<str_pair> v2;
v1.insert("EUR");
v1.insert("JPY");
v1.insert("GBP");
v1.insert("USD");
v1.insert("NOK");
v1.insert("XXX");
v2.insert(str_pair("EUR", "EIBEUR"));
v2.insert(str_pair("EUR", "EOIEUR"));
v2.insert(str_pair("EUR", "LIBEUR"));
v2.insert(str_pair("GPB", "LIBGBP"));
v2.insert(str_pair("ZZZ", "LIBZZZ"));
cout << "v1:" << endl;
copy( v1.begin(), v1.end(), ostream_iterator<string>(std::cout, " - "));
cout << endl << "v2:" << endl;
copy( v2.begin(), v2.end(), ostream_iterator<str_pair>(std::cout, " - "));
cout << endl << endl;
set<str_pair> v3;
set_intersection( v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v3), MyComp );
cout << "v3:" << endl;
copy( v3.begin(), v3.end(), ostream_iterator<str_pair>(std::cout, " - "));
cin.get();
return 0;
} |
Partager