Punchcard

Untitled

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