Advertisement
SansPapyrus683

C++ Debugging Template

Dec 11th, 2021
1,678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <deque>
  4. #include <map>
  5. #include <unordered_map>
  6. #include <set>
  7. #include <unordered_set>
  8.  
  9. template <typename T>
  10. std::ostream& operator<<(std::ostream& out, const std::vector<T>& vec) {
  11.     if (vec.empty()) {
  12.         out << "[]";
  13.         return out;
  14.     }
  15.     out << '[';
  16.     for (int i = 0; i < vec.size() - 1; i++) {
  17.         out << vec[i] << ", ";
  18.     }
  19.     return out << vec.back() << ']';
  20. }
  21.  
  22. template <typename T1, typename T2>
  23. std::ostream& operator<<(std::ostream& out, const std::pair<T1, T2>& pair) {
  24.     return out << '(' << pair.first << ", " << pair.second << ')';
  25. }
  26.  
  27. template <typename T>
  28. std::ostream& operator<<(std::ostream& out, const std::deque<T>& deq) {
  29.     if (deq.empty()) {
  30.         out << "[]";
  31.         return out;
  32.     }
  33.     out << '[';
  34.     for (int i = 0; i < deq.size() - 1; i++) {
  35.         out << deq[i] << ", ";
  36.     }
  37.     return out << deq.back() << ']';
  38. }
  39.  
  40. template <typename T1, typename T2>
  41. std::ostream& operator<<(std::ostream& out, const std::unordered_map<T1, T2>& map) {
  42.     out << '{';
  43.     for (auto it = map.begin(); it != map.end(); it++) {
  44.         std::pair<T1, T2> element = *it;
  45.         out << element.first << ": " << element.second;
  46.         if (std::next(it) != map.end()) {
  47.             out << ", ";  
  48.         }
  49.     }
  50.     return out << '}';
  51. }
  52.  
  53. template <typename T1, typename T2>
  54. std::ostream& operator<<(std::ostream& out, const std::map<T1, T2>& map) {
  55.     out << '{';
  56.     for (auto it = map.begin(); it != map.end(); it++) {
  57.         std::pair<T1, T2> element = *it;
  58.         out << element.first << ": " << element.second;
  59.         if (std::next(it) != map.end()) {
  60.             out << ", ";  
  61.         }
  62.     }
  63.     return out << '}';
  64. }
  65.  
  66. template <typename T>
  67. std::ostream& operator<<(std::ostream& out, const std::unordered_set<T>& set) {
  68.     out << '{';
  69.     for (auto it = set.begin(); it != set.end(); it++) {
  70.         T element = *it;
  71.         out << element;
  72.         if (std::next(it) != set.end()) {
  73.             out << ", ";  
  74.         }
  75.     }
  76.     return out << '}';
  77. }
  78.  
  79. template <typename T>
  80. std::ostream& operator<<(std::ostream& out, const std::multiset<T>& set) {
  81.     out << '{';
  82.     for (auto it = set.begin(); it != set.end(); it++) {
  83.         T element = *it;
  84.         out << element;
  85.         if (std::next(it) != set.end()) {
  86.             out << ", ";  
  87.         }
  88.     }
  89.     return out << '}';
  90. }
  91.  
  92. template <typename T>
  93. std::ostream& operator<<(std::ostream& out, const std::unordered_multiset<T>& set) {
  94.     out << '{';
  95.     for (auto it = set.begin(); it != set.end(); it++) {
  96.         T element = *it;
  97.         out << element;
  98.         if (std::next(it) != set.end()) {
  99.             out << ", ";  
  100.         }
  101.     }
  102.     return out << '}';
  103. }
  104.  
  105. template <typename T>
  106. std::ostream& operator<<(std::ostream& out, const std::set<T>& set) {
  107.     out << '{';
  108.     for (auto it = set.begin(); it != set.end(); it++) {
  109.         T element = *it;
  110.         out << element;
  111.         if (std::next(it) != set.end()) {
  112.             out << ", ";  
  113.         }
  114.     }
  115.     return out << '}';
  116. }
  117.  
  118. // this part is copied from SO
  119. template<typename Type, unsigned N, unsigned Last>
  120. struct TuplePrinter {
  121.     static void print(std::ostream& out, const Type& value) {
  122.         out << std::get<N>(value) << ", ";
  123.         TuplePrinter<Type, N + 1, Last>::print(out, value);
  124.     }
  125. };
  126.  
  127. template<typename Type, unsigned N>
  128. struct TuplePrinter<Type, N, N> {
  129.     static void print(std::ostream& out, const Type& value) {
  130.         out << std::get<N>(value);
  131.     }
  132. };
  133.  
  134. template<typename... Types>
  135. std::ostream& operator<<(std::ostream& out, const std::tuple<Types...>& value) {
  136.     out << '(';
  137.     TuplePrinter<std::tuple<Types...>, 0, sizeof...(Types) - 1>::print(out, value);
  138.     return out << ')';
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement