/** * * @file Adaptor.cpp * * @authors M. Laporte, D. Mathieu * * @date 07/12/2011 * * @version V1.0 * **/ #include <string> #include <vector> #include <algorithm> // sort() #include <iostream> #include <functional> using namespace std; namespace { template <typename T1, typename T2, typename TRes> class IBinaryFunction { public : typedef T1 first_argument_type; typedef T2 second_argument_type; typedef TRes result_type; virtual ~IBinaryFunction (void) {} virtual TRes operator () (const T1 &, const T2 &) const = 0; }; // IBinaryFunction class Pers { string myNom; unsigned myAge; public : Pers (const string & Nom, unsigned Age) : myNom (Nom), myAge (Age) {} const string & getNom (void) const noexcept { return myNom; } unsigned getAge (void) const noexcept { return myAge; } private : ostream & display (ostream & os) const { return os << getAge () << " - " << getNom (); } // display() public : friend ostream & operator << (ostream & os, const Pers & p) { return p.display (os); } }; // Pers class TriParAgeAsc : public IBinaryFunction <Pers, Pers, bool> { public : virtual ~TriParAgeAsc (void) noexcept {} virtual bool operator () (const Pers & p1, const Pers & p2) const noexcept { return p1.getAge () < p2.getAge (); } // operator () }; // TriParAgeAsc class TriParNomDesc : public IBinaryFunction <Pers, Pers, bool> { public : virtual ~TriParNomDesc (void) noexcept {} virtual bool operator () (const Pers & p1, const Pers & p2) const noexcept { return p1.getNom () > p2.getNom (); } // operator () }; // TriParNomDesc void adaptor (void) { cout << "FunctorSort : \n"; typedef vector <Pers> CVPers; CVPers vPers; vPers.push_back ( Pers ("Charlotte", 21)); vPers.push_back ( Pers ("Alfred", 12)); vPers.push_back ( Pers ("Jean", 42)); vPers.push_back ( Pers ("Noemie", 11)); vPers.push_back ( Pers ("Berthe", 99)); vPers.push_back ( Pers ("Agathe", 29)); vPers.push_back ( Pers ("Sylvain", 42)); vPers.push_back ( Pers ("Pierre", 75)); cout << "\nTri par age croissant\n\n"; sort (vPers.begin (), vPers.end (), TriParAgeAsc ()); for (const Pers & personne : vPers) cout << personne << '\n'; cout << "\nTri par nom decroissant\n\n"; sort (vPers.begin (), vPers.end (), TriParNomDesc ()); for (const Pers & personne : vPers) cout << personne << '\n'; cout << "\nRecherche de la premiere personne d'age <= 40 : "; CVPers::const_iterator pos = find_if (vPers.begin (), vPers.end (), bind2nd (TriParAgeAsc (), Pers (" ",40)));// a completer if (vPers.end () == pos) cout << "Aucun element correspondant\n"; else cout << *pos << '\n'; cout << "\nRecherche de la premiere personne d'age<= 4 : "; pos = find_if (vPers.begin (), vPers.end (), bind2nd (TriParAgeAsc (), Pers (" ", 4)));// a completer if (vPers.end () == pos) cout << "Aucun element correspondant\n"; else cout << *pos << '\n'; } // adaptor() } // namespace int main (void) { adaptor (); return 0; } // main()