Ever wanted to look in console at variables you use? Meet these 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.
/*-----------------------------------------------*/
dbgm()
DeBuG Multiple
You can print the names of several variables first and values next:
string s = {"codeforces"};
int t = 5; char u = 'R';
pair<pair<double, unsigned int>, pair<int, string>> v = {{234.34534, 42}, {133, "IOI"}};
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!
Thanks to this post and this suggestion by HosseinYousefi
Full version of the dbg*() library is here. For printing tuples I used the code from this blog
UPD1: a link to the full library added
UPD2: tuple printing added
I'd rather use my shitty debugging skills which I understand than using something cryptic like this.
true , lol but still we should appreciate the poster for trying to help community.
Well, I just wanted to share my shitty debugging skills
One of your "four lines" has more than 2000 characters...
I would also like to thank MikeMirzayanov for the great Codeforces Custom invocation which doesn't have automatic line breaking
Look at my beautiful debugger! It's less than 1600 now!
While your function seems to work, why did you obfuscate everything in a single line? Good luck changing the function in case you have a bug or want to add something new to it...
Because I provided extended code in the post. And it's easier to copy-paste 4 lines of code, than 42
Every time I see the things people use to debug in C++ (how did this ever pass review I wonder?), I'm glad I switched to D quite a while ago.
Result:
Perhaps in 20 years, the C++ committee will agree on a feasible means of debug output to have in the standard library. But life, or ICPC eligibility, or whatever other contests, they are here and now.
Moses said
While I agree with the sentiment that C++ is changing too slowly in some areas where it should be changing faster, code you linked can be trivially written to support any number of args without copypaste
You are right, the strange addition to
testlib.h
could perhaps be rewritten as a recursive template to accept any number of arguments. And it will be cleaner and shorter. And it may look trivial once it's done.However, the mere existence of this commit shows that doing it "right" still requires some effort: learning a bit more of C++ features and/or making sure the result compiles on a bunch of compiler versions.
Alright, there's no point I'm trying to make here, just an observation.
You are gonna save a lot of time of guys like us, in the upcoming journey of CP.
Thanks a lot.
laughs in rust
I left my team
Anyway, you will never have this in a formal competition.
.
Sanitator, Its very helpful. I came across a
bug
, hope you fix it soon.Frankly speaking, currently I can't come up with idea how to not manually pass the sizes of dimensions of a variable sized array to
dbg()
, so the only thing I could suggest is to set constant sizes of dimensions of arrays at compile time. Any ideas without specific functions for 2D, 3D... arrays?Do you have an idea for 1D arrays?
I wonder where were LanceTheDragonTrainer
I got notified of this post because you tagged me in it today.
Since it's not practical to actually type this code, we should have it somewhere in our template and use it in online contests like codeforces.
So why not using a complete header file like PrettyPrint?
Put it in your directory and then add couple of lines in your code, something like:
Note: GDB has built-in pretty-printing of data structures. So if you know how to use GDB it would be much easier.
Using GDB also have the advantage of being able to print user-defined struct without defining operator<< for them.
It's recommended to check the environment before the real contest day, because it's possible that you need to explicitly turn them on to use them, or there are multiple versions of GDB installed and not all of them have the feature. (See for example https://stackoverflow.com/questions/4985414/how-to-enable-gdb-pretty-printing-for-c-stl-objects-in-eclipse-cdt . However if
gdb_printers
can't be found in the machine then you're out of luck)C++ macro debug support color, line number, print pair, stl container...
For linux, windows 10 enable virtual terminal processing