Advertisement
RobertDeMilo

Теория. Универсальные функции вывода контейнеров

Oct 28th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. template <typename First, typename Second>
  9. ostream& operator<<(ostream& out, const pair<First, Second>& p) {
  10.     return out << '(' << p.first << ", "s << p.second << ')';
  11. }
  12.  
  13. template <typename Container>
  14. void Print(ostream& out, const Container& container) {
  15.     bool is_first = true;
  16.     for (const auto& element : container) {
  17.         if (!is_first) {
  18.             out << ", "s;
  19.         }
  20.         is_first = false;
  21.         out << element;
  22.     }
  23. }
  24.  
  25. template <typename Element>
  26. ostream& operator<<(ostream& out, const vector<Element>& container) {
  27.     out << '[';
  28.     Print(out, container);
  29.     out << ']';
  30.     return out;
  31. }
  32.  
  33. template <typename Element>
  34. ostream& operator<<(ostream& out, const set<Element>& container) {
  35.     out << '{';
  36.     Print(out, container);
  37.     out << '}';
  38.     return out;
  39. }
  40.  
  41. template <typename Key, typename Value>
  42. ostream& operator<<(ostream& out, const map<Key, Value>& container) {
  43.     out << '<';
  44.     Print(out, container);
  45.     out << '>';
  46.     return out;
  47. }
  48.  
  49. int main() {
  50.     setlocale(LC_ALL, "ru");
  51.  
  52.     const set<string> cats = { "Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s };
  53.     cout << cats << endl;
  54.  
  55.     const vector<int> ages = { 10, 5, 2, 12 };
  56.     cout << ages << endl;
  57.  
  58.     const map<string, int> cat_ages = {
  59.         {"Мурка"s, 10},
  60.         {"Белка"s, 5},
  61.         {"Георгий"s, 2},
  62.         {"Рюрик"s, 12}
  63.     };
  64.     cout << cat_ages << endl;
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////
  68. Мои попыточки
  69. #include <iostream>
  70. #include <vector>
  71. #include <map>
  72. #include <set>
  73.  
  74. using namespace std;
  75.  
  76. template <typename First, typename Second>
  77. ostream& operator<<(ostream& out, const pair<First, Second>& p) {
  78.     return out << '(' << p.first << ", "s << p.second << ')';
  79. }
  80.  
  81. //с const работать не будет!
  82. template <typename Container>
  83. ostream& operator<<(ostream& out, Container& container) {
  84.  
  85.     bool is_first = true;
  86.     for (const auto& element : container) {
  87.         if (!is_first) {
  88.             out << ", "s;  без s работать не будет!
  89.         }
  90.         is_first = false;
  91.         out << element;
  92.     }
  93.     return out;
  94. }
  95.  
  96.  
  97. int main() {
  98.     //для строк не работает
  99.     const set<string> cats = { "Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s };
  100.     cout << cats << endl;
  101.  
  102.     const set<int> ages = { 10, 5, 2, 12 };
  103.     cout << ages << endl;
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement