shushus

Спринт 2. Универсальные функции вывода контейнеров. Задание 6.

Dec 8th, 2022
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. template <typename Key, typename Value>
  10. ostream& Print (ostream& out, const map<Key, Value>& container) {
  11.     /*for(const auto& Key: container){
  12.         Print(out, Key);
  13.     }*/
  14.     bool checker = true;
  15.     for (const auto& [key, value] : container) {
  16.         if(checker==false){
  17.         out<<", ";
  18.         }
  19.         checker=false;
  20.         out << "("s<<key<<", "s<<value<<")"s;
  21.     }
  22.     return out;
  23. }
  24.  
  25. template <typename Container>
  26. ostream& Print (ostream& out, const Container& container){
  27.     /*int counter = 1, last_index = container.size();
  28.     for (const auto& element : container) {
  29.         if(counter == last_index){
  30.             out << element;
  31.         }else{
  32.         out << element<< ", "s;
  33.         }
  34.         ++counter;
  35.     }
  36.     return out;*/
  37.     bool checker = true;
  38.     for (const auto& element : container) {
  39.         if (checker==false) {
  40.             out << ", "s;
  41.         }
  42.         checker = false;
  43.         out << element;
  44.     }
  45.     return out;
  46. }
  47.  
  48. template <typename Key, typename Value>
  49. ostream& operator<<(ostream& out, const map<Key, Value>& container) {
  50.     return out<<"<"s, Print(out, container), out<<">"s;
  51. }
  52.  
  53. template <typename Container>
  54. ostream& operator<<(ostream& out, const vector<Container>& container) {
  55.     return out<<"["s, Print(out, container), out<<"]"s;
  56. }
  57.  
  58.  
  59. template <typename Container>
  60. ostream& operator<<(ostream& out, const set<Container>& container) {
  61.     return out<<"{"s, Print(out, container), out<<"}"s;
  62. }  
  63.  
  64.  
  65.  
  66.  
  67.    
  68. /*int main() {
  69.     const vector<string> cats = {"Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s};
  70.     cout << cats << endl;
  71. }*/
  72.  
  73. int main() {
  74. const set<string> cats = {"Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s};
  75. cout << cats << endl;
  76. const vector<int> ages = {10, 5, 2, 12};
  77. cout << ages << endl;
  78. const map<string, int> cat_ages = {
  79.     {"Мурка"s, 10},
  80.     {"Белка"s, 5},
  81.     {"Георгий"s, 2},
  82.     {"Рюрик"s, 12}
  83. };
  84. cout << cat_ages << endl;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment