Advertisement
Alaricy

вывод контейнеров 6/6 затык с мап

Oct 29th, 2022 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <map>
  5. using namespace std;
  6.  
  7. template <typename Key, typename Value>
  8. ostream& operator<<(ostream& out, const pair<Key, Value>& frommap) {
  9. {
  10. out<<"("<<frommap.first<<", "<<frommap.second<<")";
  11. }
  12.     return out;  
  13. }
  14.  
  15. template <typename print>
  16. ostream& Print(ostream& out, const print& container){
  17.     bool isfirst=1;
  18.     for (const auto& element : container) {
  19.     if (isfirst)  out << ""s;
  20.     else out << ", "s;
  21.     isfirst=0;
  22.     out << element << ""s;
  23.     }
  24.   return out;
  25. }
  26.  
  27. template <typename invect>
  28. ostream& operator<<(ostream& out, const vector<invect>& container) {
  29. out<<"[";
  30. Print(out, container);
  31. out<<"]";
  32. return out;
  33. }
  34.  
  35. template <typename inset>
  36. ostream& operator<<(ostream& out, const set<inset>& container) {
  37. out<<"{";
  38. Print(out, container);
  39. out<<"}";
  40.     return out;    
  41. }
  42.  
  43. template <typename Key, typename Value>
  44. ostream& operator<<(ostream& out, const map<Key, Value>& container) {
  45. out<<"<";
  46. for (auto from_map: container){
  47. Print(out, from_map);
  48. }
  49. out<<">";
  50.     return out;    
  51. }
  52.  
  53.  
  54.  
  55. int main() {
  56.     const set<string> cats = {"Мурка"s, "Белка"s, "Георгий"s, "Рюрик"s};
  57. cout << cats << endl;
  58. const vector<int> ages = {10, 5, 2, 12};
  59. cout << ages << endl;
  60. const map<string, int> cat_ages = {
  61.     {"Мурка"s, 10},
  62.     {"Белка"s, 5},
  63.     {"Георгий"s, 2},
  64.     {"Рюрик"s, 12}
  65. };
  66. cout << cat_ages << endl;
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement