Sanitator's blog

By Sanitator, history, 8 months 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 Swift

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

»
8 months 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.

  • »
    »
    8 months 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.

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

    Well, I just wanted to share my shitty debugging skills

»
8 months ago, # |
  Vote: I like it +157 Vote: I do not like it

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

  • »
    »
    8 months 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

  • »
    »
    8 months 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!

»
8 months 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...

  • »
    »
    8 months 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

»
8 months 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.

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

    Moses said

  • »
    »
    7 months 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

    • »
      »
      »
      7 months 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.

»
8 months 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.

»
8 months 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

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

I left my team

»
8 months ago, # |
  Vote: I like it +14 Vote: I do not like it

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

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

.

»
8 months 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
  • »
    »
    8 months 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
»
8 months ago, # |
  Vote: I like it +8 Vote: I do not like it

I wonder where were LanceTheDragonTrainer

»
7 months 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