Hi guys,
Many of you would've used a very handy function in Java that takes in a variable number of arguments (of possibly different types) and outputs them.
void debug(Object...args)
{
System.out.println(Arrays.deepToString(args));
}
I've written some C++ code that behaves in an exactly identical manner that you can put in your C++ template as well.
Features :
1) Nifty and quick debug statements
2) It can also output some STL containers like vector, set and maps in a pretty and easy to read format.
3) If you compile your code with DEBUG flag, these debug statements are executed, else all these statements are removed from the code. So you don't even need to strip them off manually by commenting or deleting. In particular at any online judge, none of these statements would be executed.
Usage :
So to compile a file and use debug statements, you just need to write g++ -DDEBUG a.cpp .If you dont give this flag and compile in ordinary way like : g++ a.cpp, then these statements are removed from the code.
You can find the code here. You just need to copy lines 10 to 91. I've tested it on GCC, though I expect it to work with other compilers as well.
Merry Christmas & Happy Coding :)
for example we have vector<int> a, consisting of number [0..5]; // 0 1 2 3 4 5
if I write debug(a) -
debugger now shows like this: [0, 1, 2, 3, 4, 5].
it would be better if it shows like this: a = [0, 1, 2, 3, 4, 5]. (I mean to showing the name of variable).
Hope it helps :)
#define debug_var(x) debug(#x ": ", x)
So
debug_var(2+2);
will be compiled to
debug("2+2" ": ", 2+2);
I'm not aware of any way in which I could make debug(A,B) print as "A : (A) , "B : (B)
Here is a code I use (it is from some blog on codeforces, sorry I don't remember by whom), IDE to play:
For example,
outputs
It has limitations of course, but I find it very convenient and customizable. For example it does not parse calls correctly, e.g.
EVARS(gcd(a,b))
results in#40: gcd(A=138;
Need some help ...i was solving previous codeforces contest problem 1199C - MP3 .... when i submitted the code with the debugging statements 58073974 i was getting TLE ...but after some time just randomly I comment down the debugging statement and surprisingly I got the accepted verdict 58074310 ....in just 328 sec can anyone explain me is these statements affect this much ??
This is a very common thing. Your trace function is sending output data to the standard error stream through cerr. Outputting data consumes execution time of the program. Codeforces checker will read data output data only from stdout, which is output through cout. But the data that is going to stderr by cerr is still going there, which consumes execution time and hence the code TLEs. So, always remember to comment the line
#define TRACE
in your code before submitting or to comment every line that calls trace() function so that it doesn't happen.thanks
"hello" ----> 45
Lines 74 - 87 are defining how a map should be printed. You can rewrite that to suit your needs.
In particular you'd need to rewrite line 83 as :
os << "(" << (*ii).first << " ---> " << (*ii).second <<")"
Win it this time! :)
Hi, any chance you can upload this again or upload it elsewhere? The link just leads to a blank page for me on both Firefox and Chrome :/
Cheers, Esuhi
His last visit was 18 months ago, so I don't think he will see your comment.
The code is not there, Can anybody share the code again? please ...
Code is available at archive.org: https://web.archive.org/web/20130515161027/http://ideone.com/MxA5B
Can someone please post link? The mentionded ideone link isn't working.
https://www.ideone.com/3X0NPy take it
Do i need to create DEBUG and ONLINE_JUDGE variables(global) too? because there's no DEBUG and ONLINE_JUDGE variable.
no , you can copy from 14th line till 87th .
I am using visual studio code as described here: https://medium.com/@dcrystalj/debugging-c-c-code-in-visual-studio-code-vsc-f42fde4c418
Can I ask you guys a really really stupid question (^^)? How can I use it with Codeblock compile engine. (Like if I need to press some other button, and if click "Build and Run" print the debug lines out ? ).