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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
   | #include <algorithm>
#include <string>
#include <iostream>
#include <iterator> // juste pour le ostream_iterator, pour l'affichage
#include <vector>
 
using namespace std;
 
 
struct str_pair // pourrait etre une std::pair<string,string>
{
    str_pair( const string & first, const string & second ) : first_( first ), second_( second ) {} 
    string first_, second_;
};
 
// juste pour l'affichage
ostream& operator << ( ostream & ostr, const str_pair & pair_in )
{
    ostr << pair_in.first_ << "/" << pair_in.second_;
    return ostr;
}
 
struct Construct // foncteur pour construire le resultat
{
    Construct( const vector<string> & vref, vector<str_pair> & result ) : vref_( vref ), result_( result ) {}
 
    void operator () ( const str_pair & sp )
    {
        if ( find( vref_.begin(), vref_.end(), sp.first_ ) != vref_.end() )
            result_.push_back( sp );
    }
 
    const vector<str_pair> & GetResult() const { return result_; }
 
private:
    vector<str_pair> & result_;
    const vector<string> & vref_;
};
 
int main(int argc, char* argv[])
{
    vector<string> v1;
    v1.push_back("EUR");
    v1.push_back("JPY");
    v1.push_back("GBP");
    v1.push_back("USD");
    v1.push_back("NOK");
    v1.push_back("XXX");
 
    vector<str_pair> v2;
    v2.push_back(str_pair("EUR", "EIBEUR"));
    v2.push_back(str_pair("EUR", "EOIEUR"));
    v2.push_back(str_pair("EUR", "LIBEUR"));
    v2.push_back(str_pair("GPB", "LIBGBP"));
    v2.push_back(str_pair("ZZZ", "LIBZZZ"));
 
    // affichage v1
    cout << "v1:" << endl;
    copy( v1.begin(), v1.end(), ostream_iterator<string>(std::cout, " - "));
 
    // affichage v2
    cout << endl << "v2:" << endl;
    copy( v2.begin(), v2.end(), ostream_iterator<str_pair>(std::cout, " - "));
    cout << endl << endl;
 
    // construction resultat
    vector<str_pair> v3;
    for_each( v2.begin(), v2.end(), Construct( v1, v3 ) );
 
    // affichage resultat
    cout << "v3:" << endl;
    copy( v3.begin(), v3.end(), ostream_iterator<str_pair>(std::cout, " - "));
 
    cin.get();
    return 0;
} |