Advertisement
Guest User

suce moi

a guest
May 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. /**
  6. *
  7. * @file FunctorSort.cpp
  8. *
  9. * @authors M. Laporte, D. Mathieu
  10. *
  11. * @date 07/12/2011
  12. *
  13. * @version V1.0
  14. *
  15. **/
  16. #include <iostream>
  17. #include <string>
  18. #include <algorithm> // sort(), find_if()
  19. #include <vector>
  20.  
  21. using namespace std;
  22.  
  23. namespace
  24. {
  25. template <typename T>
  26. class IPredicatGen
  27. {
  28. public :
  29. virtual ~IPredicatGen (void) {}
  30. virtual bool operator () (const T &) const = 0;
  31.  
  32. }; // ILessThanGen
  33.  
  34. template <typename T>
  35. class ILessThanGen
  36. {
  37. public :
  38. virtual ~ILessThanGen (void) {}
  39. virtual bool operator () (const T &, const T &) const = 0;
  40.  
  41. }; // ILessThanGen
  42.  
  43.  
  44. class Pers
  45. {
  46. string myNom;
  47. unsigned myAge;
  48.  
  49. public :
  50. Pers (const string & Nom, unsigned Age)
  51. : myNom (Nom), myAge (Age) {}
  52.  
  53. const string & getNom (void) const noexcept { return myNom; }
  54. unsigned getAge (void) const noexcept { return myAge; }
  55.  
  56. private :
  57. ostream & display (ostream & os) const
  58. {
  59. return os << getAge () << " - " << getNom ();
  60.  
  61. } // display()
  62.  
  63. public :
  64. friend ostream & operator << (ostream & os, const Pers & p)
  65. {
  66. return p.display (os);
  67.  
  68. }
  69.  
  70. }; // Pers
  71.  
  72. class SelParTrancheAge : public IPredicatGen <Pers>
  73. {
  74. unsigned myAgeMin;
  75. unsigned myAgeMax;
  76. public:
  77. SelParTrancheAge (unsigned ageMin, unsigned ageMax): myAgeMax (ageMax), myAgeMin (ageMin) {}
  78. virtual bool operator () (const Pers & p1) const noexcept
  79. {
  80. if (p1.getAge() >= myAgeMin && p1.getAge() <= myAgeMax) return true;
  81. return false;
  82. }
  83. virtual ~SelParTrancheAge () {}
  84. };// SelParTrancheAge
  85.  
  86. class SelParNomMin : public IPredicatGen <Pers>
  87. {
  88. string myNomMin;
  89. public:
  90. SelParNomMin (string NomMin): myNomMin (NomMin){}
  91. virtual bool operator () (const Pers & p1) const noexcept
  92. {
  93. if (p1.getNom() > myNomMin) return true;
  94. return false;
  95. }
  96. virtual ~SelParNomMin () {}
  97. };// SelParNomMin
  98.  
  99. class TriParAgeAsc : public ILessThanGen <Pers>
  100. {
  101. public :
  102. virtual ~TriParAgeAsc (void) noexcept {}
  103.  
  104. virtual bool operator () (const Pers & p1, const Pers & p2)
  105. const noexcept
  106. {
  107. return p1.getAge () <= p2.getAge ();
  108.  
  109. } // operator ()
  110.  
  111. }; // TriParAgeAsc
  112.  
  113. class TriParNomDesc : public ILessThanGen <Pers>
  114. {
  115. public :
  116. virtual ~TriParNomDesc (void) noexcept {}
  117.  
  118. virtual bool operator () (const Pers & p1, const Pers & p2)
  119. const noexcept
  120. {
  121. return p1.getNom () >= p2.getNom ();
  122.  
  123. } // operator ()
  124.  
  125. }; // TriParNomDesc
  126.  
  127. void functorSort (void)
  128. {
  129. cout << "FunctorSort : \n";
  130.  
  131. typedef vector <Pers> CVPers;
  132.  
  133. CVPers vPers;
  134.  
  135. vPers.push_back ( Pers ("Charlotte", 21));
  136. vPers.push_back ( Pers ("Alfred", 12));
  137. vPers.push_back ( Pers ("Jean", 42));
  138. vPers.push_back ( Pers ("Noemie", 11));
  139. vPers.push_back ( Pers ("Berthe", 99));
  140. vPers.push_back ( Pers ("Agathe", 29));
  141. vPers.push_back ( Pers ("Sylvain", 42));
  142. vPers.push_back ( Pers ("Pierre", 75));
  143.  
  144. cout << "\nTri par age croissant\n\n";
  145.  
  146. sort (vPers.begin (), vPers.end (), TriParAgeAsc ());
  147.  
  148. for (const Pers & personne : vPers)
  149. cout << personne << '\n';
  150.  
  151. cout << "\nTri par nom decroissant\n\n";
  152.  
  153. sort (vPers.begin (), vPers.end (), TriParNomDesc ());
  154.  
  155. for (const Pers & personne : vPers)
  156. cout << personne << '\n';
  157.  
  158. } // functorSort()
  159.  
  160.  
  161. void functorFind ()
  162. {
  163. cout << "\n FunctorFind: \n" << endl;
  164.  
  165. typedef vector <Pers> CVPers;
  166. CVPers vPers;
  167.  
  168. vPers.push_back ( Pers ("Charlotte", 21));
  169. vPers.push_back ( Pers ("Alfred", 12));
  170. vPers.push_back ( Pers ("Jean", 42));
  171. vPers.push_back ( Pers ("Noemie", 11));
  172. vPers.push_back ( Pers ("Berthe", 99));
  173. vPers.push_back ( Pers ("Agathe", 29));
  174. vPers.push_back ( Pers ("Sylvain", 42));
  175. vPers.push_back ( Pers ("Pierre", 75));
  176.  
  177. for (const Pers & personne : vPers)
  178. cout << personne << '\n';
  179.  
  180. CVPers::const_iterator pos;
  181.  
  182. cout << "\nRecherche sur 43 <= age <= 75 : ";
  183.  
  184. pos = find_if (vPers.begin (), vPers.end (),
  185. SelParTrancheAge (43, 75));
  186. if (vPers.end () == pos)
  187. cout << "Aucun element ne correspond a ce critere\n";
  188. else
  189. cout << *pos << '\n';
  190.  
  191. cout << "\nRecherche sur 43 <= age <= 45 : ";
  192.  
  193. pos = find_if (vPers.begin (), vPers.end (),
  194. SelParTrancheAge (43, 45));
  195. if (vPers.end () == pos)
  196. cout << "Aucun element ne correspond a ce critere\n";
  197. else
  198. cout << *pos << '\n';
  199.  
  200. cout << '\n';
  201.  
  202. cout << "\nRecherche sur nom > Noemie : ";
  203.  
  204. pos = find_if (vPers.begin (), vPers.end (), SelParNomMin ("Noemie"));
  205.  
  206. if (vPers.end () == pos)
  207. cout << "Aucun element ne correspond a ce critere\n";
  208. else
  209. cout << *pos << '\n';
  210.  
  211. cout << "\nRecherche sur nom > alfred: ";
  212.  
  213. pos = find_if (vPers.begin (), vPers.end (), SelParNomMin("alfred"));
  214.  
  215. if (vPers.end () == pos)
  216. cout << "Aucun element ne correspond a ce critere\n";
  217. else
  218. cout << *pos << '\n';
  219.  
  220. } // functorFind()
  221.  
  222. } // namespace
  223.  
  224. int main (void)
  225. {
  226. functorSort();
  227. functorFind();
  228.  
  229. return 0;
  230.  
  231. } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement