Skip to content

Instantly share code, notes, and snippets.

@Tagussan
Last active April 2, 2022 11:10
Show Gist options
  • Save Tagussan/c57f62f6a8e92aa30f22c113efaa896b to your computer and use it in GitHub Desktop.
Save Tagussan/c57f62f6a8e92aa30f22c113efaa896b to your computer and use it in GitHub Desktop.
C++ debug header. Use -DDBUG_ option when compiling. Many thanks for https://www.creativ.xyz/dump-cpp-652/
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
// vector
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "{";
for (unsigned long i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
}
os << "}";
return os;
}
// vector of vector
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &vec) {
os << "{";
for (unsigned long i = 0; i < vec.size(); i++) {
os << "{";
for (unsigned long j = 0; j < vec[i].size(); j++) {
os << vec[i][j] << (j + 1 == vec[i].size() ? "" : ", ");
}
os << "}" << (i + 1 == vec.size() ? "" : ", ");
}
os << "}";
return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
os << "{";
repi(itr, map_var) {
os << *itr;
itr++;
if (itr != map_var.end()) os << ", ";
itr--;
}
os << "}";
return os;
}
// set
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
os << "{";
repi(itr, set_var) {
os << *itr;
itr++;
if (itr != set_var.end()) os << ", ";
itr--;
}
os << "}";
return os;
}
#define DUMPOUT cerr
void dump_func() {
DUMPOUT << endl;
}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail) {
DUMPOUT << head;
if (sizeof...(Tail) > 0) {
DUMPOUT << ", ";
}
dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define dump(...) \
DUMPOUT << "[" << __FUNCTION__ << ":" << to_string(__LINE__) << "]" \
<< " " << string(#__VA_ARGS__) << " = ", \
dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment