Advertisement
karlicoss

я был вынужден

Aug 28th, 2011
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <set>
  4. #include <algorithm>
  5. #include <typeinfo>
  6. #include <map>
  7. #include <iostream>
  8. #include <fstream>
  9. using namespace std;
  10.  
  11. namespace my
  12. {
  13.  
  14.  
  15. template<typename T>
  16. string typestr(T)
  17. {
  18.     return "undefined";
  19. }
  20.  
  21. string typestr(int)
  22. {
  23.     return "int";
  24. }
  25.  
  26. string typestr(bool)
  27. {
  28.     return "bool";
  29. }
  30.  
  31. template<typename T>
  32. string typestr(const set<T> &);
  33.  
  34. template<typename T>
  35. string typestr(const vector<T> &);
  36.  
  37. template<typename A, typename B>
  38. string typestr(const pair<A, B> &);
  39.  
  40. template<typename K, typename V>
  41. string typestr(const map<K, V> &);
  42.  
  43.  
  44. template<typename T>
  45. string typestr(const set<T> &s)
  46. {
  47.     T t;
  48.     return "set<" + typestr(t) + ">";
  49. }
  50.  
  51. template<typename T>
  52. string typestr(const vector<T> &v)
  53. {
  54.     T t;
  55.     return "vector<" + typestr(t) + ">";
  56. }
  57.  
  58. template<typename A, typename B>
  59. string typestr(const pair<A, B> &p)
  60. {
  61.     return "pair<" + typestr(p.first) + ", " + typestr(p.second) + ">";
  62. }
  63.  
  64. template<typename K, typename V>
  65. string typestr(const map<K, V> &m)
  66. {
  67.     K k;
  68.     V v;
  69.     return "map<" + typestr(k) + ", " + typestr(v) + ">";
  70. }
  71.  
  72. template<typename T>
  73. ostream & operator<<(ostream &ofs, const vector<T> &v)
  74. {
  75.     ofs << "{" + typestr(v) + ": ";
  76.     for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it)
  77.     {
  78.         ofs << *it << " ";
  79.     }
  80.     ofs << "}";
  81.     return ofs;
  82. }
  83.  
  84. template<typename T>
  85. ostream & operator<<(ostream &ofs, const set<T> &s)
  86. {
  87.     ofs << "{" + typestr(s) + ": ";
  88.     for (typename set<T>::const_iterator it = s.begin(); it != s.end(); ++it)
  89.     {
  90.         ofs << *it << " ";
  91.     }
  92.     ofs << "}";
  93.     return ofs;
  94. }
  95.  
  96. template<typename K, typename V>
  97. ostream & operator<<(ostream &ofs, const map<K, V> &m)
  98. {
  99.     ofs << "{" + typestr(m) + ": ";
  100.     for (typename map<K, V>::const_iterator it = m.begin(); it != m.end(); ++it)
  101.     {
  102.         ofs << *it << " ";
  103.     }
  104.     ofs << "}";
  105.     return ofs;
  106. }
  107.  
  108. template<typename A, typename B>
  109. ostream & operator<<(ostream &ofs, const pair<A, B> &p)
  110. {
  111.     ofs << "{" + typestr(p) + ": ";
  112.     ofs << p.first << ", " << p.second;
  113.     ofs << "}";
  114.     return ofs;
  115.  
  116. }
  117.  
  118. }
  119.  
  120.  
  121. int main()
  122. {
  123.     using namespace my;
  124.     pair<set<int>, vector<map<bool, double> > > p;
  125.     cout << p;
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement