Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 2.52 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C   To filter a class vector using algorithm
  2. bool checkCountry (string x, string y)
  3. {
  4.   return (x == y);
  5. }
  6. vector<Student> studentList;
  7. studentList.push_back(Student("Tom", 'M', "91213242", "America"));
  8. studentList.push_back(Student("Jessilyn", 'F', "98422333", "Europe"));
  9.        
  10. using std::copy_if;
  11. using std::ostream_iterator;
  12. using std::cout;
  13.  
  14. enum Region {
  15.     AMERICA,
  16.     EUROPE,
  17. };
  18.  
  19. bool is_american(const Student& student)
  20. {
  21.     return student.isFrom(AMERICA);
  22. }
  23.  
  24. copy_if(students.begin(), students.end(),
  25.         ostream_iterator<Student>(cout, "n"),
  26.         is_american);
  27.        
  28. void show_students_from_region(const Region& region)
  29. {
  30.     copy_if(students.begin(), students.end(),
  31.             ostream_iterator<Student>(cout, "n"),
  32.             [](const Student& student) { return student.isFrom(region); });
  33. }
  34.        
  35. struct checkCountry
  36. {
  37.   std::string country;
  38.   bool operator()(const Student& x)
  39.   {
  40.     return (x.country() == country);
  41.   }
  42. };
  43.  
  44. int main()
  45. {
  46.   std::vector<Student> studentList;
  47.   studentList.push_back(Student("Tom", 'M', "91213242", "America"));
  48.   studentList.push_back(Student("Jessilyn", 'F', "98422333", "Europe"));
  49.  
  50.   typedef boost::filter_iterator<checkCountry, std::vector<Student>::iterator> FilterIter;
  51.   checkCountry predicate;
  52.   predicate.country = "America";
  53.   FilterIter filter_iter_first(predicate, studentList.begin(), studentList.end());
  54.   FilterIter filter_iter_last(predicate,  studentList.end(),  studentList.end());
  55.  
  56.   std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator<Student>(std::cout, " "));
  57. }
  58.        
  59. struct country_filter
  60. {
  61.     country_filter(const std::string& a_country): country(a_country) {}
  62.     void operator()(const Student& a_s) const
  63.     {
  64.         if (country == a_s.country)
  65.         {
  66.             std::cout << a_s.name << "n";
  67.         }
  68.     }
  69.     std::string country;
  70. };
  71.  
  72. //
  73. std::for_each(studentList.begin(), studentList.end(), country_filter("Ireland"));
  74.        
  75. std::string country = "America";
  76. std::for_each(studentList.begin(), studentList.end(), [&country] (const Student& a_s)
  77. {
  78.     if (a_s.country == country)
  79.     {
  80.         std::cout << a_s.name << "n";
  81.     }
  82. });
  83.        
  84. struct checkCountry {
  85.   const string& compare;
  86.   checkCountry(const string& compare) : compare(compare) {}
  87.   bool operator()(const string& x) { return x == compare; }
  88. };
  89.  
  90. vector<Student> studentList;
  91. studentList.push_back(Student("Tom", 'M', "91213242", "America"));
  92. studentList.push_back(Student("Jessilyn", 'F', "98422333", "Europe"));
  93. howMany = std::count_if(studentList.begin(), studentList.end(), checkCountry("America"));