shushus

Спринт 12. Идиома immediately invoked lambda expression.

Apr 6th, 2023
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. template <typename Iterator>
  10. class IteratorRange {
  11. public:
  12.     IteratorRange(Iterator begin, Iterator end) : first(begin)
  13.                                                 , last(end) {}
  14.  
  15.     Iterator begin() const {
  16.         return first;
  17.     }
  18.  
  19.     Iterator end() const {
  20.         return last;
  21.     }
  22.  
  23. private:
  24.     Iterator first, last;
  25. };
  26.  
  27. template <typename Collection>
  28. auto Head(Collection& v, size_t top) {
  29.     return IteratorRange{v.begin(),
  30.                          next(v.begin(), min(top, v.size()))};
  31. }
  32.  
  33. struct Person {
  34.     string name;
  35.     int age, income;
  36.     bool is_male;
  37. };
  38.  
  39. vector<Person> ReadPeople(istream& input) {
  40.     int count;
  41.     input >> count;
  42.  
  43.     vector<Person> result(count);
  44.     for (Person& p : result) {
  45.         char gender;
  46.         input >> p.name >> p.age >> p.income >> gender;
  47.         p.is_male = gender == 'M';
  48.     }
  49.  
  50.     return result;
  51. }
  52.  
  53. int main() {
  54.     vector<Person> people = ReadPeople(cin);
  55.  
  56.     sort(people.begin(), people.end(),
  57.         [](const Person& lhs, const Person& rhs) {
  58.         return lhs.age < rhs.age;
  59.     });
  60.  
  61.     for (string command; cin >> command;) {
  62.        
  63.         if (command == "AGE"s) {
  64.             int adult_age;
  65.             cin >> adult_age;
  66.  
  67.             auto adult_begin = lower_bound(people.begin(), people.end(), adult_age,
  68.                 [](const Person& lhs, int age) {
  69.                 return lhs.age < age;
  70.             });
  71.  
  72.             cout << "There are " << distance(adult_begin, people.end())
  73.                  << " adult people for maturity age " << adult_age << '\n';
  74.            
  75.         } else if (command == "WEALTHY"s) {
  76.             int count;
  77.             cin >> count;
  78.  
  79.             auto head = Head(people, count);
  80.  
  81.             partial_sort(head.begin(), head.end(), people.end(),
  82.                 [](const Person& lhs, const Person& rhs) {
  83.                 return lhs.income > rhs.income;
  84.             });
  85.  
  86.             int total_income = accumulate(head.begin(), head.end(), 0, [](int cur, const Person& p) {
  87.                 return cur += p.income;
  88.             });
  89.            
  90.             cout << "Top-"s << count << " people have total income "s << total_income << '\n';
  91.            
  92.         } else if (command == "POPULAR_NAME"s) {
  93.             char gender;
  94.             cin >> gender;
  95.  
  96.             IteratorRange range{people.begin(), partition(people.begin(), people.end(),
  97.                                 [gender](const Person& p) {
  98.                                     return p.is_male == (gender == 'M');
  99.                                 })};
  100.            
  101.             if (range.begin() == range.end()) {
  102.                 cout << "No people of gender "s << gender << '\n';
  103.                
  104.             } else {
  105.                
  106.                 sort(range.begin(), range.end(),
  107.                     [](const Person& lhs, const Person& rhs) {
  108.                     return lhs.name < rhs.name;
  109.                 });
  110.                
  111.                 const string* most_popular_name = &range.begin()->name;
  112.                 int count = 1;
  113.                
  114.                 for (auto i = range.begin(); i != range.end();) {
  115.                     auto same_name_end = find_if_not(i, range.end(),
  116.                         [i](const Person& p) {
  117.                         return p.name == i->name;
  118.                     });
  119.                    
  120.                     auto cur_name_count = distance(i, same_name_end);
  121.                     if (cur_name_count > count) {
  122.                         count = cur_name_count;
  123.                         most_popular_name = &i->name;
  124.                     }
  125.                     i = same_name_end;
  126.                 }
  127.                 cout << "Most popular name among people of gender "s << gender << " is "s << *most_popular_name << '\n';
  128.             }
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment