Praktikum_mf

Задача 6

Dec 14th, 2021
2,719
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 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.     const set<string> cats = {"Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s};
  51.     cout << cats << endl;
  52.  
  53.     const vector<int> ages = {10, 5, 2, 12};
  54.     cout << ages << endl;
  55.  
  56.     const map<string, int> cat_ages = {
  57.         {"Мурка"s, 10},
  58.         {"Белка"s, 5},
  59.         {"Георгий"s, 2},
  60.         {"Рюрик"s, 12}
  61.     };
  62.     cout << cat_ages << endl;
  63. }
Advertisement
Comments
  • nochnoikoshmar1
    3 years (edited)
    # C++ 1.57 KB | 0 0
    1. template <typename Element, typename T>
    2. void Print(ostream &out, const map<Element, T> &container){
    3.     int count = 1;
    4.     for(const auto &[element,age] : container){
    5.         out << "("s << element << ", " << age << ")";
    6.         if(container.size() > count){
    7.             out << ", "s;
    8.             ++count;
    9.         }      
    10.     }
    11. }
    12. template <typename Element>
    13. void Print(ostream &out, const Element &container){
    14.     bool is_map = true;
    15.     for(const auto &element : container){
    16.         if(!is_map)
    17.             out << ", "s;        
    18.         is_map = false;
    19.         out << element;
    20.     }
    21. }
    22. template <typename Element>
    23. ostream &operator<<(ostream &out, const vector<Element> &container){
    24.     out << "["s;
    25.     Print(out, container);
    26.     out << "]"s;
    27.     return out;
    28. }
    29. template <typename Element>
    30. ostream &operator<<(ostream &out, const set<Element> &container){
    31.     out << "{"s;
    32.     Print(out, container);
    33.     out << "}"s;
    34.     return out;
    35. }
    36. template <typename Element, typename T>
    37. ostream &operator<<(ostream &out, const map<Element,T> &container){    
    38.     out << "<"s;
    39.     Print(out, container);
    40.     out << ">"s;
    41.     return out;
    42. }
    43. int main(){
    44.     setlocale(LC_ALL, "");
    45.     const set<string> cats = {"Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s};
    46.     cout << cats << endl;
    47.     const vector<int> ages = {10, 5, 2, 12};
    48.     cout << ages << endl;
    49.     const map<string, int> cat_ages = {
    50.         {"Мурка"s, 10},
    51.         {"Белка"s, 5},
    52.         {"Георгий"s, 2},
    53.         {"Рюрик"s, 12}
    54.     };
    55.     cout << cat_ages << endl;
    56. }
  • W_A_S_A_B_I
    3 years
    # C++ 0.06 KB | 0 0
    1. Вы забыли подключить:
    2. #include <string>
  • delix777
    2 years
    # C++ 1.61 KB | 0 0
    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. }
Add Comment
Please, Sign In to add comment