Praktikum_mf

Задача 2

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