Ever wanted to look in console at variables you use? Meet three helper functions!
dbg()
DeBuG
Suppose we have some nested containers(vector, string, bitset, set, map
) or arrays, which for simplicity we may consider a multidimensional array. dbg()
can neatly print the name, bounds and, at last, the values from the required sub-array with automatic bound checking:
For example:
int j[2][2][3] = {{{4,5,6},{10,11,12}}, {{1,2,3}, {7,8,9}}};
dbg(j);
dbg(j, 0,0, 0,1, 0,1);
output:
[[[4, 5, 6],
[10, 11, 12]],
[[1, 2, 3],
[7, 8, 9]]]
[[[4, 5],
[10, 11]]]
You pass the name of array and [two closed bounds] for each dimension(btw, you can omit several last bounds). If they are too large, dbg()
reduces them. By default the bounds are set on the start and the end of each dimension.
+If you pass bounds [l;r]
to the dimension that is map or set, the output goes from the lth largest to the rth largest key, or to the last element of dimension(if r is too big).
+dbg()
works with c-arrays whose sizes of dimensions are constant and known at compile time.
/*-----------------------------------------------*/
dbgs()
DeBuG with Spaces
It may also be helpful to print just the values of variables:
string s = {"codeforces"};
int t = 5; char u = 'R';
pair<pair<double, unsigned int>, pair<int, string>> v = {{234.34534, 42}, {133, "IOI"}};
dbgs(s,t,u,v); cout << endl;
output:
"codeforces" 5 R ((234.345340, 42), (133, "IOI"))
+Strings are printed inside double quotes "" for debug purposes. You can change it in definition of to_string(string s);
/*-----------------------------------------------*/
dbgm()
DeBuG Multiple
You can print the names first and values next:
dbgm(s,t,u,v);
output:
[s,t,u,v]: "codeforces" 5 R ((234.345340, 42), (133, "IOI"))
/*-----------------------------------------------*/
Here's my code. It's hugely inspired by this submission by tourist.
The compact version is created from the extended one by means of http://removelinebreaks.net/.
/*-----------------------------------------------*/
Hope these functions save your precious minutes during contests. Enjoy!
UPD: dbgs(), dbgm() added