Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm> // sort()
- #include <vector>
- #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 functorSort (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)));
- 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)));
- if (vPers.end () == pos)
- cout << "Aucun element correspondant\n";
- else
- cout << *pos << '\n';
- } // functorSort()
- } // namespace
- int main (void)
- {
- functorSort ();
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement