Advertisement
kutuzzzov

Уро 4 Указатели и константность

Dec 7th, 2022
1,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. // Породы кошек
  10. enum class CatBreed {
  11.     Bengal,
  12.     Balinese,
  13.     Persian,
  14.     Siamese,
  15.     Siberian,
  16.     Sphynx,SuccessSuccess
  17. };
  18.  
  19. // Пол
  20. enum class Gender {
  21.     Male,
  22.     Female,
  23. };
  24.  
  25. struct Cat {
  26.     string name;
  27.     Gender gender;
  28.     CatBreed breed;
  29.     int age;
  30. };
  31.  
  32. string CatBreedToString(CatBreed breed) {
  33.     switch (breed) {
  34.         case CatBreed::Bengal:
  35.             return "Bengal"s;
  36.         case CatBreed::Balinese:
  37.             return "Balinese"s;
  38.         case CatBreed::Persian:
  39.             return "Persian"s;
  40.         case CatBreed::Siamese:
  41.             return "Siamese"s;
  42.         case CatBreed::Siberian:
  43.             return "Siberian";
  44.         case CatBreed::Sphynx:
  45.             return "Sphynx"s;
  46.         default:
  47.             throw invalid_argument("Invalid cat breed"s);
  48.     }
  49. }
  50.  
  51. ostream& operator<<(ostream& out, CatBreed breed) {
  52.     out << CatBreedToString(breed);
  53.     return out;
  54. }
  55.  
  56. ostream& operator<<(ostream& out, Gender gender) {
  57.     out << (gender == Gender::Male ? "male"s : "female"s);
  58.     return out;
  59. }
  60.  
  61. ostream& operator<<(ostream& out, const Cat& cat) {
  62.     out << '{' << cat.name << ", "s << cat.gender;
  63.     out << ", breed: "s << cat.breed << ", age:"s << cat.age << '}';
  64.     return out;
  65. }
  66.  
  67. template <typename Comparator>
  68. vector<const Cat*> GetSortedCats(const vector<Cat>& cats, const Comparator& comp) {
  69.     vector<const Cat*> sorted_cat_pointers;
  70.     for (const Cat& item : cats) {
  71.         sorted_cat_pointers.push_back(&item);
  72.     }
  73.    
  74.     sort(sorted_cat_pointers.begin(), sorted_cat_pointers.end(), [comp](const Cat* lhs, const Cat* rhs) {
  75.            return comp(*lhs, *rhs);
  76.            });
  77.  
  78.     return sorted_cat_pointers;
  79. }
  80.  
  81. // Выводит в поток out значения объектов, на который ссылаются указатели вектора cat_pointers.
  82. // Пример вывода элементов vector<const Cat*>:
  83. // {{Tom, male, breed: Bengal, age:2}, {Charlie, male, breed: Balinese, age:7}}
  84. void PrintCatPointerValues(const vector<const Cat*>& cat_pointers, ostream& out) {
  85.     // Напишите функцию самостоятельно
  86.     if (cat_pointers.empty()) {
  87.         out << "{}";
  88.     } else {
  89.         bool is_first = ", ";
  90.         out << "{"s;
  91.         for (const Cat* item_ptr : cat_pointers) {
  92.             if (is_first) {
  93.                 out << *item_ptr; is_first = !", ";
  94.             } else {
  95.                 out << ", " << *item_ptr;
  96.             }
  97.         }
  98.         out << "}"s;
  99.     }
  100. }
  101.  
  102. int main() {
  103.     const vector<Cat> cats = {
  104.         {"Tom"s, Gender::Male, CatBreed::Bengal, 2},
  105.         {"Leo"s, Gender::Male, CatBreed::Siberian, 3},
  106.         {"Luna"s, Gender::Female, CatBreed::Siamese, 1},
  107.         {"Charlie"s, Gender::Male, CatBreed::Balinese, 7},
  108.         {"Ginger"s, Gender::Female, CatBreed::Sphynx, 5},
  109.         {"Tom"s, Gender::Male, CatBreed::Siamese, 2},
  110.     };
  111.  
  112.     {
  113.         auto sorted_cats = GetSortedCats(cats, [](const Cat& lhs, const Cat& rhs) {
  114.             return tie(lhs.breed, lhs.name) < tie(rhs.breed, rhs.name);
  115.         });
  116.  
  117.         cout << "Cats sorted by breed and name:"s << endl;
  118.         PrintCatPointerValues(sorted_cats, cout);
  119.         cout << endl;
  120.     }
  121.  
  122.     {
  123.         auto sorted_cats = GetSortedCats(cats, [](const Cat& lhs, const Cat& rhs) {
  124.             return tie(lhs.gender, lhs.breed) < tie(rhs.gender, rhs.breed);
  125.         });
  126.  
  127.         cout << "Cats sorted by gender and breed:"s << endl;
  128.         PrintCatPointerValues(sorted_cats, cout);
  129.         cout << endl;
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement