Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm> // sort()
- #include <vector>
- using namespace std;
- namespace{
- 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
- template <typename T>
- class IPredicatGen{
- public :
- virtual ~IPredicatGen (void) {}
- virtual bool operator () (const T &) const = 0;
- }; // ILessThanGen
- template <typename T, typename TRes>
- class IUnaryFunction{
- public :
- virtual ~IUnaryFunction (void) {}
- virtual TRes operator () (const T &) const = 0;
- }; // ILessThanGen
- class SelParTrancheAge: public IUnaryFunction <Pers,bool>{
- unsigned int myAgeMin;
- unsigned int myAgeMax;
- public :
- SelParTrancheAge(unsigned ageMin, unsigned ageMax): myAgeMin(ageMin), myAgeMax(ageMax){}
- virtual ~SelParTrancheAge (void) {}
- virtual bool operator () (const Pers & pers1) const{
- return (pers1.getAge() >= myAgeMin && pers1.getAge() <= myAgeMax);
- }
- }; // SelParTrancheAge
- class SelParNomMin : public IUnaryFunction <Pers,bool>{
- string myNomMin;
- public :
- SelParNomMin(string nomMin) : myNomMin(nomMin) {}
- virtual ~SelParNomMin() {}
- virtual bool operator () (const Pers & pers1) const{
- return pers1.getNom() > myNomMin;
- }
- };
- class TriParAgeAsc : public IPredicatGen <Pers>{
- 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 IPredicatGen <Pers>{
- public :
- virtual ~TriParNomDesc (void) noexcept {}
- virtual bool operator () (const Pers & p1, const Pers & p2)const noexcept{
- return p1.getNom () >= p2.getNom ();
- } // operator ()
- }; // TriParNomDesc
- void functorFind (void){
- cout << "FunctorFind : \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));
- for (const Pers & personne : vPers)
- cout << personne << '\n';
- CVPers::const_iterator pos;
- cout << "\nRecherche sur 43 <= age <= 75 : ";
- pos = find_if (vPers.begin (), vPers.end (), SelParTrancheAge(43,75)); // a completer
- if (vPers.end () == pos)
- cout << "Aucun element ne correspond a ce critere\n";
- else
- cout << *pos << '\n';
- cout << "\nRecherche sur 43 <= age <= 45 : ";
- pos = find_if (vPers.begin (), vPers.end (), SelParTrancheAge(43,45));
- if (vPers.end () == pos)
- cout << "Aucun element ne correspond a ce critere\n";
- else
- cout << *pos << '\n';
- cout << '\n';
- cout << "\nRecherche sur nom > Noemie: ";
- pos = find_if (vPers.begin (), vPers.end (), SelParNomMin("Noemie"));
- if (vPers.end () == pos)
- cout << "Aucun element ne correspond a ce critere\n";
- else
- cout << *pos << '\n';
- cout << "\nRecherche sur nom > alfred: ";
- pos = find_if (vPers.begin (), vPers.end (), SelParNomMin("alfred"));
- if (vPers.end () == pos)
- cout << "Aucun element ne correspond a ce critere\n";
- else
- cout << *pos << '\n';
- } // functorFind()
- } // namespace
- int main (void)
- {
- functorFind ();
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement