Hi everyone, I'm wondering if people who often participate in competitions, who are qualified in competitive programming, how to debug bugs? By couting everything (C++), or using the codeblock's debug tool?
# | User | Rating |
---|---|---|
1 | Benq | 3783 |
2 | jiangly | 3772 |
3 | tourist | 3706 |
4 | maroonrk | 3609 |
5 | Um_nik | 3591 |
6 | fantasy | 3526 |
7 | ko_osaga | 3500 |
8 | inaFSTream | 3477 |
9 | cnnfls_csy | 3427 |
10 | zh0ukangyang | 3423 |
# | User | Contrib. |
---|---|---|
1 | Um_nik | 185 |
2 | awoo | 182 |
3 | nor | 172 |
4 | -is-this-fft- | 170 |
5 | adamant | 169 |
6 | maroonrk | 165 |
7 | antontrygubO_o | 160 |
8 | SecondThread | 159 |
9 | dario2994 | 152 |
9 | kostka | 152 |
Hi everyone, I'm wondering if people who often participate in competitions, who are qualified in competitive programming, how to debug bugs? By couting everything (C++), or using the codeblock's debug tool?
Name |
---|
cout all variables
thank bro:))
You can use this-->modify as per need
ifndef ONLINE_JUDGE
define debug(x) cerr <<#x<<" ";_print(x);cerr<<endl;
else
define debug(x)
endif
void _print(int a){cerr<<a;} void _print(char a){cerr<<a;} void _print(string a){cerr<<a;} void _print(bool a){cerr<<a;}
template void _print(vector v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template void _print(set v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template void _print(multiset v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
now just write debug(variable name) and create a separate debugging file to read the output
That mostly depends on what makes you feel comfortable. Printing everything provides a bunch of information at once, without having to do every step. Using a debugger can help understand the process leading to the bug and its cause.
Most people choose to print because it's just straightforward. However, some use a debugger (even with gdb in the terminal).
thank you so much
There are a lot of methods for finding errors inside the written code. Here are 2 main ones:
Using a debugger. Most of the IDEs provided at the olympiads have this tool. Most of all, I personally like the GDB. It is present in CLion, Code::Blocks, DevC++. It is not difficult to understand it, but it gives great opportunities in finding errors. I advise you to watch the following video — https://www.youtube.com/watch?v=J7L2x1ATOgk. In addition, if you experience memory leaks or a segmentation error, then I advise you to use valgrind (https://valgrind.org/docs/manual/quick-start.html) (a method not for traveling olympiads).
Stress testing. A very important skill if debugging doesn't help. I found a good article on the Russian Algorithmica — https://algorithmica.org/ru/stress-test, I hope Google translator can handle it. You can also use https://cfstress.com to search for cf tests, however this service requires a subscription for full use.
I strongly do not recommend using any cout << x to check what is currently in this variable. This not only clutters up your code, but also does not give a complete understanding of what is happening in the program.
thank you ~~