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

Full text and comments »

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