Advertisement
RobertDeMilo

YB1.9 Рефакторим код и улучшаем читаемость вывода

Oct 27th, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. в моей студио почему - то не запускатеся ? !
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <map>
  6. #include <sstream>
  7. #include <utility>
  8. #include <fstream>
  9.  
  10. using namespace std;
  11.  
  12. template <typename Collection>
  13. string Join(const Collection& c, char d)
  14. {
  15.     stringstream ss;            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  16.  
  17.     bool first = true; // флаг, который говорит первый мы выводим элемент или нет
  18.     for (const auto& i : c)
  19.     {
  20.         if (!first)
  21.         {
  22.             ss << d;            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  23.         }
  24.  
  25.         first = false;
  26.         ss << i;                !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  27.     }
  28.  
  29.     return ss.str();            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  30. }
  31.  
  32. template <typename First, typename Second>
  33. ostream& operator<< (ostream& out, const pair<First, Second>& p)
  34. {
  35.     return out << '(' << p.first << ',' << p.second << ')';
  36. }
  37.  
  38. template <typename T>
  39. ostream& operator<< (ostream& out, const vector<T>& vi)
  40. {
  41.     return out << '[' << Join(vi, ',') << ']';
  42. }
  43.  
  44. template <typename Key, typename Value>
  45. ostream& operator<< (ostream& out, const map<Key, Value>& m)
  46. {
  47.     return out << '{' << Join(m, ',') << '}';
  48. }
  49.  
  50. int main()
  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.  
  58.     vector<vector<int>> v = { {1, 2}, {3, 4} };
  59.     cout << v << endl;
  60.  
  61.     vector <string> w = { "helo", "world", "me" };
  62.     cout << w << endl;
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement