Praktikum_mf

Задача 1

Dec 14th, 2021
2,136
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 1 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. struct Animal {
  9.     string name;
  10.     int age;
  11.     double weight;
  12. };
  13.  
  14. template <typename Container, typename KeyMapper>
  15. void SortBy(Container& container, KeyMapper key_mapper) {
  16.     sort(container.begin(), container.end(),
  17.          [key_mapper](const auto& lhs, const auto& rhs) {
  18.             return key_mapper(lhs) < key_mapper(rhs);
  19.          });
  20. }
  21.  
  22. void PrintNames(const vector<Animal>& animals) {
  23.     for (const Animal& animal : animals) {
  24.         cout << animal.name << ' ';
  25.     }
  26.     cout << endl;
  27. }
  28.  
  29. int main() {
  30.     vector<Animal> animals = {
  31.         {"Мурка"s,   10, 5},
  32.         {"Белка"s,   5,  1.5},
  33.         {"Георгий"s, 2,  4.5},
  34.         {"Рюрик"s,   12, 3.1},
  35.     };
  36.  
  37.     PrintNames(animals);
  38.  
  39.     SortBy(animals, [](const Animal& animal) { return animal.name; });
  40.     PrintNames(animals);
  41.  
  42.     SortBy(animals, [](const Animal& animal) { return -animal.weight; });
  43.     PrintNames(animals);
  44. }
Advertisement
Comments
  • delix777
    2 years
    # C++ 1.19 KB | 0 0
    1. #include <algorithm>
    2. #include <iostream>
    3. #include <vector>
    4.  
    5. using namespace std;
    6.  
    7. struct Animal {
    8.     string name;
    9.     int age;
    10.     double weight;
    11. };
    12.  
    13. template <typename Container, typename KeyMapper>
    14. void SortBy(Container& container, KeyMapper key_mapper) {
    15.     // теперь можно сортировать контейнер
    16.     // с компаратором key_mapper(lhs) < key_mapper(rhs)
    17.     sort(container.begin(), container.end(), [key_mapper](const auto& lhs, const auto& rhs){
    18.         return key_mapper(lhs) < key_mapper(rhs);
    19.     });
    20.    
    21. }
    22.  
    23. void PrintNames(const vector<Animal>& animals) {
    24.     for (const Animal& animal : animals) {
    25.         cout << animal.name << ' ';
    26.     }
    27.     cout << endl;
    28. }
    29.  
    30. int main() {
    31.     vector<Animal> animals = {
    32.         {"Мурка"s, 10, 5},
    33.         {"Белка"s, 5, 1.5},
    34.         {"Георгий"s, 2, 4.5},
    35.         {"Рюрик"s, 12, 3.1},
    36.     };
    37.  
    38.     PrintNames(animals);
    39.  
    40.     SortBy(animals, [](const Animal& animal) {
    41.         return animal.name;
    42.     });
    43.     PrintNames(animals);
    44.  
    45.     SortBy(animals, [](const Animal& animal) {
    46.         return -animal.weight;
    47.     });
    48.     PrintNames(animals);
    49.  
    50.     return 0;
    51. }
Add Comment
Please, Sign In to add comment