My debugging template is:
#define ts to_string
string ts(char c) { return string(1, c); }
string ts(bool b) { return b ? "true" : "false"; }
string ts(const char* s) { return (string)s; }
string ts(string s) { return s; }
template<class A> string ts(complex<A> c) {
stringstream ss; ss << c; return ss.str(); }
string ts(vector<bool> v) {
string res = "{"; for(int i = 0; i < si(v); ++i) res += char('0' + v[i]);
res += "}"; return res; }
template<size_t SZ> string ts(bitset<SZ> b) {
string res = ""; for(int i = 0; i < SZ; ++i) res += char('0' + b[i]);
return res; }
template<class A, class B> string ts(pair<A,B> p);
template<class T> string ts(T v) {
bool fst = 1; string res = "{";
for (const auto& x: v) {
if (!fst) res += ", ";
fst = 0; res += ts(x);
}
res += "}"; return res;
}
template<class A, class B> string ts(pair<A,B> p) {
return "(" + ts(p.f) + ", " + ts(p.s) + ")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << ts(h); if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
When I type
dbg(a, n)
where 'a' is the vector name and n is the size of the vector. 'a' contains the following {1, 2, 3, 4, 5} and n = 5
it prints
[a, n]: [{1, 2, 3, 4, 5}, 5]
but I want it to print
[a]: [{1, 2, 3, 4, 5}]
[n]: [5]
without having to type
dbg(a);
dbg(n);
Is there any way to do this?
Here is code that does what you want. DBG takes a string with the names separated by commas as the first parameter, and the variables as the remaining parameters. Then it prints the first name and the first variables, and passes the rest recursively to DBG.
What am I supposed to put in the function DBG?
DBG takes the comma separated names of the variables, and then the variables as parameters.
You just need to replace this part of your code with the code I gave.
Thanks! This is the new code that I am working with:
dbg
convertsdbg(a, n);
todbg(a);
dbg(n);
edbg
doesn't changedbg(a, n)
I use the following macros if need debugging:
Sample usage:
Output:
x: -1, y: 2, z: -3
All you need is to overload the stream output operators for vector<>, pair<>, etc. as you want to.