Praktikum_mf

Задача 4

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