Today, I was seeing tourist's submissions. He uses a very cool way to print the variables for the purpose of debugging. The function debug_out
lists all variable names, followed by their values. I found it very interesting and convenient. However, when you would like to print a long list of variables, it might be hard to match variables and their corresponding values. I made a few changes, and I would like to share the code with you.
Please note that you need to define XOX
(-D XOX
should do the job). Therefore, when you are submitting your code, you do not need to remove or comment out lines including debug
. Hope you'll find it useful.
#include <bits/stdc++.h>
using namespace std;
vector<string> vec_splitter(string s) {
for(char& c: s) c = c == ','? ' ': c;
stringstream ss; ss << s;
vector<string> res;
for(string z; ss >> z; res.push_back(z));
return res;
}
void debug_out(vector<string> args, int idx) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, Head H, Tail... T) {
if(idx > 0) cerr << ", ";
stringstream ss; ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, T...);
}
#ifdef XOX
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__)
#else
#define debug(...) 42
#endif
int main() {
int x = -1, y = 10000;
double z = 0.2;
string s = "beginner1010";
long long b = 1LL << 60;
debug(x, y, z, s, b);
return 0;
}
Results:
x = -1, y = 10000, z = 0.2, s = beginner1010, b = 1152921504606846976