Advertisement
RobertDeMilo

YB1.8 Универсальные функции вывода контейнеров в поток

Oct 27th, 2023
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <sstream>
  5. #include <utility>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. ************************************************************************
  11. template <typename T>
  12. ostream& operator<< (ostream& out, const vector<T>& vi)
  13. {
  14.     for (const auto& i : vi)
  15.     {
  16.         out << i << ' ';
  17.     }
  18.     return out;
  19. }
  20. ************************************************************************
  21.  
  22. template <typename First, typename Second>
  23. ostream& operator<< (ostream& out, const pair<First, Second>& p)
  24. {
  25.     out << p.first << ',' << p.second;
  26.     return out;
  27. }
  28. ************************************************************************
  29.  
  30. template <typename Key, typename Value>
  31. ostream& operator<< (ostream& out, const map<Key, Value>& m)
  32. {
  33.     for (const auto& i : m)
  34.     {
  35.         out << i << ' ';
  36.     }
  37.     return out;
  38. }
  39. ************************************************************************
  40.  
  41. int main()
  42. {
  43.     /*ifstream fin;
  44.     ofstream of;
  45.     fstream f;
  46.  
  47.     istringstream g;
  48.     ostringstream h;
  49.     stringstream s;*/
  50.     //мои
  51.  
  52.     vector<double> vi = { 1.4,2,3 };
  53.     cout << vi << endl;
  54.  
  55.     map<int, double> m = { {1,2.5}, {3,4} };
  56.     cout << m << endl;
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement