STL question
Can somebody please suggest more efficient way to solve that task:
We have map<string, string> test and set<string> test1
The result set<string> test2 should contain values from test which are exists in test1.
I've tried the set_intersection algorithm, but for some reason it was placing extra-elements in resulting set,so i used the for_each algorithm.
We have map<string, string> test and set<string> test1
The result set<string> test2 should contain values from test which are exists in test1.
I've tried the set_intersection algorithm, but for some reason it was placing extra-elements in resulting set,so i used the for_each algorithm.
#include <iostream> #include <string> #include <map> #include <set> #include <vector> #include <algorithm> using namespace std; typedef string type; typedef map< type, type > typemap; typedef set< type > typeset; class compare { typeset &ts; typeset &ins; public: compare( typeset &s, typeset &target ) : ts( s ), ins( target ) {} void operator() ( pair<type, type> it ) { if ( ts.find( it.second ) != ts.end() ) ins.insert( it.second ); } }; class print { public: void operator() ( const type what ) { cout << what << ","; } }; int main() { typemap test; test[ "123" ] = "1"; test[ "124" ] = "2"; test[ "125" ] = "3"; test[ "126" ] = "4"; test[ "127" ] = "5"; test[ "128" ] = "6"; test[ "129" ] = "7"; typeset test1; test1.insert( "1" ); test1.insert( "3" ); test1.insert( "5" ); test1.insert( "7" ); test1.insert( "9" ); typeset test2; for_each ( test.begin(), test.end(), compare( test1, test2 ) ); for_each( test2.begin(), test2.end(), print() ); cout << endl; }
