Advertisement
kutuzzzov

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

Nov 17th, 2021
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4.  
  5. using namespace std;
  6.  
  7. template <typename Element>
  8. ostream& operator<<(ostream& out, const vector<Element>& container) {
  9. bool is_first = ", ";
  10.     for (const Element& element : container) {
  11.         if (is_first) {
  12.             out << element; is_first = !", ";
  13.         } else {
  14.             out << ", " << element;
  15.         }
  16.     }
  17.     return out;
  18. }  
  19.  
  20. template <typename Element>
  21. ostream& operator<<(ostream& out, const set<Element>& container) {
  22. bool is_first = ", ";
  23.     for (const Element& element : container) {
  24.         if (is_first) {
  25.             out << element; is_first = !", ";
  26.         } else {
  27.             out << ", " << element;
  28.         }
  29.     }
  30.     return out;
  31. }  
  32.  
  33. int main() {
  34.     const set<string> cats = {"Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s};
  35.     cout << cats << endl;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement