3

Ultimate C++ Debug Template

 7 months ago
source link: https://codeforces.com/blog/entry/125435
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Ultimate C++ Debug Template

Frustrated with keeping track of variables in different functions? Say goodbye to the hassle with our easy-to-use template!
It's designed to handle all data types, making it simple to see variable statuses throughout your program.

Template
Simple Usage

This template supports datatypes such as:

  • Primitive: int, char, bool, long long int etc.
  • STL: pair, tuple, vector, set, map, stack, queue, priority_queue, bitset etc.
  • Arrays of all datatypes: int arr[], bool arr[], vector<int> adj[] etc.
  • Matrix: int mat[100][200], vector<vector<bool>> vis(100, vector<bool> (200, 0)) etc.
  • Decayed Arrays / Matrices.
  • Rvalue Literals like "Hello", false, 'z', isSafe(i, j), dfs(u) etc.
  • Even complicated nested datatypes like: map<string, vector<pair<char, unordered_set<long long>>>> WHATTT; etc.

You can use it on your computer when doing coding challenges on sites like CodeForces.
Additionally, you can use it in online coding environments like LeetCode for practicing data structures and algorithms.

How to use it?

Let's say you have different datatypes such as:

Example

You can debug them like this debug(var1, var2, var3, var4, ...);

Example

In instances where arrays / matrices have decayed into pointers, use debugArr(arr, n);

What is Array Decay?

Note:

  • You don't need to remove debug(var, ...) statements in your code when submitting it.
  • On platforms like Codeforces, there's a macro called ONLINE_JUDGE that's defined, automatically disregarding all your debug statements. This ensures your solution will be accepted unless there's a logical error.
  • However, on platforms like LeetCode, ONLINE_JUDGE isn't defined. As a result, your solution might exceed the time limit even if it's correct, as these statements still consume time to print. To address this, simply change #ifndef to #ifdef before submitting, and your solution will be accepted.
  • Also, LeetCode supports cout instead of cerr, so you need to uncomment first line i.e. #define cerr cout to get output there.

How to Setup?

  • Copy this template into your own templates. The output will be directed to the stderr stream.
  • When using it for LeetCode uncomment #define cerr cout and before submitting change #ifndef to #ifdef to ignore debug(...);. For convenience, after changing it, copy it, and keep it pinned in your clipboard for repetitive use.

For Complete Beginners who need step by step tutorial (using VS Code), go to my Github, and follow these steps:

Steps

To all those people who might think that this template is too long, you change it to one liner too. If you use format on save , you can take advantage of /* clang-format off */ to keep it one line.


Since this is my first blog post, I'm open to any suggestions you might have, whether they're technical or not.

Honorable Mention to this blog, that led me to develop my own template which supports almost everything now.

If you liked this blog, please upvote it, I'd really be gratefull :)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK