When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

Sanitator's blog

By Sanitator, history, 5 years ago, translation, In English

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]]]


Another example:

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.

first example
second example


/*-----------------------------------------------*/

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

  • Vote: I like it
  • +39
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +66 Vote: I do not like it

I'd rather use my shitty debugging skills which I understand than using something cryptic like this.

  • »
    »
    5 years ago, # ^ |
    Rev. 3   Vote: I like it +20 Vote: I do not like it

    true , lol but still we should appreciate the poster for trying to help community.

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it +40 Vote: I do not like it

    Well, I just wanted to share my shitty debugging skills

»
5 years ago, # |
  Vote: I like it +157 Vote: I do not like it

One of your "four lines" has more than 2000 characters...

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +16 Vote: I do not like it

    I would also like to thank MikeMirzayanov for the great Codeforces Custom invocation which doesn't have automatic line breaking

  • »
    »
    5 years ago, # ^ |
    Rev. 4   Vote: I like it -29 Vote: I do not like it

    Look at my beautiful debugger! It's less than 1600 now!

»
5 years ago, # |
  Vote: I like it +8 Vote: I do not like it

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...

  • »
    »
    5 years ago, # ^ |
    Rev. 6   Vote: I like it +3 Vote: I do not like it

    Because I provided extended code in the post. And it's easier to copy-paste 4 lines of code, than 42

»
5 years ago, # |
  Vote: I like it +33 Vote: I do not like it

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.

example

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.

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it -16 Vote: I do not like it

    Moses said

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it +8 Vote: I do not like it

    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

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      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.

»
5 years ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

You are gonna save a lot of time of guys like us, in the upcoming journey of CP.

Thanks a lot.

»
5 years ago, # |
  Vote: I like it +25 Vote: I do not like it
let x = vec![vec![1, 2, 3], vec![4, 5]];
println!("{:?}", x);

laughs in rust

»
5 years ago, # |
Rev. 6   Vote: I like it -34 Vote: I do not like it

I left my team

»
5 years ago, # |
  Vote: I like it +14 Vote: I do not like it

Anyway, you will never have this in a formal competition.

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

.

»
5 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Sanitator, Its very helpful. I came across a bug, hope you fix it soon.

code
output
  • »
    »
    5 years ago, # ^ |
    Rev. 3   Vote: I like it +5 Vote: I do not like it

    though it has some limitations

    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?

    first option
    first option
    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Do you have an idea for 1D arrays?

»
5 years ago, # |
  Vote: I like it +8 Vote: I do not like it

I wonder where were LanceTheDragonTrainer

»
5 years ago, # |
  Vote: I like it +4 Vote: I do not like it

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:

#if !defined(ONLINE_JUDGE)
#include "prettyprint.hpp"
#endif
»
4 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

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)

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

C++ macro debug support color, line number, print pair, stl container...

  int n = 5;
  vector<int> v = {1,2,3,4};
  pair<int,string> p = {1, "codeforces"};
  debug(n, v, p);

For linux, windows 10 enable virtual terminal processing

Спойлер