delix777
Dec 4th, 2023
58
0
Never
This is comment for paste Задача 6
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. template<typename key, typename value>
  9. void Print(ostream& out, const map<key, value>& container){
  10.     bool first = true;
  11.  
  12.     for(const auto& [name, age]: container){
  13.         if(!first){
  14.             cout << ", ";
  15.         }
  16.         first = false;
  17.         out << "(" << name << ", " << age << ")";
  18.     }
  19. }
  20.  
  21. template <typename T>
  22. void Print(ostream& out, const T& container ){
  23.     bool first = true;
  24.    
  25.     for (const auto& element : container) {
  26.         if(!first){
  27.             out << ", ";
  28.         }
  29.         first = false;
  30.         out << element;
  31.     }
  32. }
  33.  
  34. template <typename T>
  35. ostream& operator<<(ostream& out, const vector<T>& container) {
  36.     out << "[";
  37.     Print(out, container);
  38.     out << "]";
  39.     return out;
  40. }
  41.  
  42. template <typename T>
  43. ostream& operator<<(ostream& out, const set<T>& container) {
  44.     out << "{";
  45.     Print(out, container);
  46.     out << "}";
  47.     return out;
  48. }
  49.  
  50. template <typename key, typename value>
  51. ostream& operator<<(ostream& out, const map<key, value>& container) {
  52.     out << "<";
  53.     Print(out, container);
  54.     out << ">";
  55.     return out;
  56. }
  57.  
  58. int main() {
  59.     const set<string> cats = {"Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s};
  60.     cout << cats << endl;
  61.     const vector<int> ages = {10, 5, 2, 12};
  62.     cout << ages << endl;
  63.     const map<string, int> cat_ages = {
  64.         {"Мурка"s, 10},
  65.         {"Белка"s, 5},
  66.         {"Георгий"s, 2},
  67.         {"Рюрик"s, 12}
  68.     };
  69.     cout << cat_ages << endl;
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment