C++ Debug Template 🛠
Simplify variable tracking across functions and loops!
Note: For latest updates and a colored stderr version of this template, visit my Github.
As of now both perform equally well, but my choice? C++20.
This template supports datatypes such as:
🔢 Primitive: int, char, bool, long long int
etc.
📚 STL: pair, tuple, vector, set, oset, map, omap, stack, queue, priority_queue, bitset
etc.
📦 Arrays of all datatypes: int arr[], bool arr[], vector<int> adj[]
etc.
🧩 Matrix: int dp[100][200], vector<vector<bool>> vis(100, vector<bool> (200, 0))
etc.
🕗 Arrays that have been decayed or declared at runtime like int arr[n]
.
📝 Rvalue Literals like "Hello", false, 'z', isSafe(i, j), dfs(u)
.
🧱 User defined structs / classes like Point
, Node
.
🤯 Even nested datatypes like map<string, vector<pair<char, unordered_set<long long>>>> WHATTT;
.
How to use it?
Let's say you have different datatypes such as:
You can debug them like this debug(var1, var2, var3, var4, ...);
If you have user defined structs / classes, you just need to make a print()
function, and use debug(...)
like you do :)
In instances where array have decayed into pointer, or you declared array at runtime, use debugArr(arr, n);
Note:
- You don't need to remove
debug(...)
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.
How to Setup?
- Copy this template into your own templates. The output will be directed to the stderr stream.
- Alternatively you can make a separate header file and include this into your template
#ifndef ONLINE_JUDGE
#include "template.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif
- When using it for LeetCode uncomment
#define cerr cout
and before submitting change#ifndef
to#ifdef
to ignoredebug(...);
. 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 (for VS Code), follow these steps:
If you liked this blog, please upvote it, I'd be really grateful :)
Unexpectedly high quality and kinda useful blog from a specialist (sounds as a ratism, yeah), great work!
I just want to note, it's probably not a good idea to copy paste this entire template into ur submission, instead store it in a header or something. Then you can do something like
Thanks for the advice. I made the appropriate changes :)
Thanks
Great post! And any way to incorporate pbds (oset, omap) would be nice...
UPD: Sorry for saying something too wrong...
My solution was to manually go into the
ext/pb_ds/assoc_container.hpp
file and add some debugs at the very bottom so u can just docout << os << '\n';
.Im not an expert with how the arguments actually work, I just know it works on my own end. If someone here has any better solutions, please share
We don't need to explicitly make one for oset/ omap, as they can be iterated through range-based for loops, so this section will print it automatically.
Okay thanks a lot
High-quality and useful content! Keep it up :)
If multiple people keep using it, won't they get plagiarism because of same template?
If the rest of your code doesn't have significant coincidence with the other person, no. But there is a small chance of it happening.
Do plagiarism detector also takes care of already published codes? Let's say a person is using already published codes of Trie or segment tree or even this template, will they get plagiarism because of it? How does that work out? Because they definitely coincide.
No, If its already available, it won't flag that.
No print tuple 😢
Thanks for pointing it out, I never used tuple so didn't think I needed that. Now I've updated the code above, try it again and it'll work.
Not Wokring for iterator
e.g:-
multiset m3; m3.insert(3); auto it2 = m3.lower_bound(1); debug(it2);
We don't debug
it
directly, instead, we debug what it is referencing.Experiment with using
debug(*it)
and it works perfectly fine.I'm just wondering, does this have any advantage over Benq's debug template ?
Definitely yes, you can test it for yourself.
Here are some Datatypes that you can test.
My template supports all these datatypes, but the following datatypes (which are commented) aren't supported by BenQ's template.
You can compare both outputs here:)
He is an ally you fools. And you are indigo too.
Viva!
WDYM ?
Great template. It would be nice if we could debug Structs as well. Is it doable?
Yes
PS: Link with implementation details and how it works [russian]
Yes, in fact you can even debug containers with your Structs as a type like
vector<Point> v
You just have to make a
print()
function, Here's how:Isn't it simple :)
I have written a blog on using a debugging template quite some time back. You all can have a look at it as well: https://cs-mshah.github.io/getting_started_with_cp/#debugging
Why does the std >= C++20 version give a CE (an error on line 14) when compiling with
-D_GLIBCXX_DEBUG
flag?Edit:
Internally
vector<bool>
is optimized and uses_Bit_reference
instead ofbool
to conserve space and provide same functionality.Therefore, we can overload
vector<bool>
itself. Issue has been fixed now. Thanks.Awesome bro.. This will surely help me a lot..
Gonna leave this here
When used for queue it clears the queue.
Thanks for pointing out. I just made a silly mistake in std=c++20.
Instead of doing
auto temp = x
, I didT temp = x
where T evaluated toqueue<int> &&q
It's now fixed :)
Edit:
Oops! Imagine the panic when contestants realize their precious queues, stacks, or priority_queues are pulling a vanishing act mid-contest!
What do I do now to fix this? I hate myself (>︿<)
Thanks for the alert, I re-downloaded the template.
Bro, Anshul_Johri "Shorter Template, (std >= C++20)" is it cause any problem in Judges where C++20 is not supported?
The Shorter Template utilizes new features from C++20. To debug it on your computer, ensure you activate the compiler flag
-std=C++20
. If you're not placing this template in a header file and it constitutes your entire source code, simply ensure that#ifndef ONLINE_JUDGE
is placed at the beginning of your template. This will allow you to use an older version of C++ when uploading your code online. However make sure to test this beforehand.I'm new to CP, though I've done DSA. I'm going to use your template in my first contest 😄
Works fine on Leetcode, Codeforces yet to be tested.
When printing vector<vector<int>>, I can see row-index, can you change it to enable to see column-index too?
It's doable, but I'm in the middle of exams right now, so maybe later?
I don't want to bother you, but can you please look into it now if your exams are over?
Probably you're not interested. Ok.
Good! I think it is a good idea to add a template for stress-testing to it.
I'm not really an expert in stress testing (no pun intended). I have simple folder with these files:
Correct.cpp
,Wrong.cpp
,Generator.cpp
,Checker.cpp
andscript.sh
I run
script.sh
from terminal, and it generatesinput.txt
,outputCorrect.txt
,outputWrong.txt
. AndChecker.cpp
compares those files. It only tells the line number, and prints the line where it failed. But it doesn't tell the test case number. I have to figure that out myself by looking atinput.txt
. Eventually I find it, but it's too time consuming and therefore I only use it when practicing. Please tell me if you've found/have a better stress tester.I have written stress-tests many times, because I like making bugs in my code. But I always write all stress tests in the same file. Recently I made a template for this:
But, in case these functions are usually very simple, like
for (int i = 0; i < n; i++) a[i] = rnd() % 100
formake_data
, it is also a good way to implement it every time you need it without writing separate functions.P.S. If it wasn't code for competitive programming, others would kill me
I never thought about stress testing within the same file. You inspired me to make one, and I did make one today. Though it uses 2 cpp files for a cleaner implementation and less clutter, it is also very easy to use (can be used during contests). I plan to make a blog about it, but only after I've tested it well enough. It also tells you the exact testcases where the code is failing. It can also work on multi answer problems (Need custom checker function though). But I also want it to work with interactive problems. I'm working on that part. Because stress testing interactive problems is a nightmare.
Great!
What about interactive problems, I have written stress test to only one of them. It is usually enough to check if the output fits some constraints, like the number of queries, and gives you correct answer.
I finally made a blog for this. I'd really appreciate if you give feedback on it there.
Edit: I deleted the blog, probably people prefer using multiple files for stress testing. But here's the code I made.
All these functions will be filled using cin, cout. No need to return anything. their outputs would be saved in stringstreams and worked upon appropriately. When we run it, it would should you the exact testcase it failed, along with expected output and received output.
C++ Stress-Test Template
Stress-Testing involves comparing your incorrect solution with a correct one to pinpoint where your code is going wrong. Normally, you don't have access to the correct solution, but if you can create a brute force solution that you're confident is accurate, then you're good to go.
I recommend you use a 2nd file to store this template for cleaner implementation.
How to use it?
Let's say you have the following problem.
You made the following solution and can't figure out why it's wrong.
Step 1: Make a brute force solution.
Step 2: Make a random test case generator
Step 3: Make sure you uncomment
// #define STRESS_TEST
to enable stress testing.Now when you run this file, you'll see an output as:
Now you realize that you didn't consider multiplication of smallest 2 elements. Hence you change it, and get Accepted
Here answer was verified using brute force solution. But what if there were multiple correct answers? How would you do then? What if instead of returning the product, we need to return it's index?
Even then it is simple, we need to tweak
testCasePassed()
function like this and our job is done.I used a very simple question to illustrate, but you get the point.
Why is it better than other stress-test templates?
input.txt
Note:
// #define STRESS_TEST
. This is what enables it in the first place.srand(time(0)
as seed to generate new random numbers everytime, but you can provide your own seed everytime however you like. Or just useuniform_int_distribution
.stderr
, so it won't interfere with anything else. If you use File I/O, make sure to havefreopen("error.txt", "w", stderr);
Do checkout my other blog C++ Debug Template!
I hope it was useful to you. Please upvote / leave a comment if you liked my blog. Thank you.
Good. I but if your solution use global varialbes, it is nice to add
clear
function which clears all global vectors, sets, etc.I have a debug template too:
Thanks
It works fine in VScode. But it does not show debug output in Leetcode. I have done all the steps mentioned in the blog for the Leetcode. What could I have done wrong?
When using it for LeetCode uncomment
#define cerr cout
and before submitting change#ifndef
to#ifdef
to ignoredebug(...)
and prevent TLE. For convenience, after changing it, copy it, and keep it pinned in your clipboard for repetitive use.It is because Leetcode doesn't support
stderr
, but it supportsstdout
See the Picture.
I have uncommented (#define cerr court) code on the clipboard.
Look, the actual code is in
template.cpp
. But when you are on LeetCode, there's notemplate.cpp
file there to access it, it's only in your local computer. When I said copy the template, I meant the actual code.Now paste this above your solution class. And it'll work fine. My leetcode submission
Now I understood. Thank you very much. This template is very helpful. Now, I can debug my code very easily than ever before.
How does type name output working?
debug(typeid(var).name());
Learn more about it here.
Thanks!
how to set up this template on sublime??
Just keep a file like
template.cpp
in your current directory which have this template.Then have this in your main file.
If you're asking about how to make a code snippet, refer this. To make snippets fast and easy, I use this website.
thanks
Thanks brother for the template :)
Can somebody help me , I can't use Cpp20 in my vs code , how can i make my vs code use cpp20 and also I am not getting colored output in "cph" STANDARD ERROR , but if i run in my terminal , then its colored , is there a way to fix it . can it be possible that i will keep using cph while the debug will happen in the terminal of vs code not in cph ,
Please can someone explain how the debug template works and why we should use it?
As they say, best way to debug is to use print statements, and OP has created a general purpose debug functions, which can print anything which is slapped inside it's arguments (pretty much). So yeah, really cool stuff!
Really cool!
small typo , give space in cpp11 file in line 162 after "||"
debug(1 << a, b) breaks it
Highly appreciated. pretty useful!!