How do you guys debug your code?

I want to know how you all debug your code. Do you use some extra variables and print commands to get the output at various checkpoints in your code or do you use any debugger like gdb or do you use vscode to debug?

Well i created my own templates .
template
debugging debug(string variable_name,debugging value)
{
cout<<variable_name<<" "<<value<<endl;
}
i call this one as debug(result,“result”);

1 Like

Or use a macro

#include <bits/stdc++.h>
using namespace std;

#define debug(var) cerr << #var << ": " << var << '\n';

int main() {
    string name = "Chef";
    int num = 100;
    float my_float = 2.5;
    debug(name);
    debug(num);
    debug(my_float);
    return 0;
}

// Output (STDERR):
name: Chef
num: 100
my_float: 2.5
1 Like
#define all(x) (x).begin(), (x).end()

void _print(auto x) {
    cerr << x << " ";
}


#ifndef ONLINE_JUDGE
#define debug(x) cerr<< #x <<"  ---> "; _print(x);cerr<<"\n";
#else
#define debug(x);
#endif


template<class T> void  _print(vector<T>v) {
    int com = 0;
    long long n = v.size();
    cerr << "[";
    for (T i : v) {
        if (com != 0) {
            cerr << ",";
        }
        com++;
        _print(i);
    }
    cerr << "]";
}

template<class T> void _print(multiset<T>s) {

    long long  com = 0;
    long long n = s.size();
    cerr << "{";
    for (T i : s) {
        if (com != 0) {
            cerr << ",";
        }
        com++;
        _print(i);
    }
    cerr << "}";
}


template<class T> void _print(set<T>s) {

    long long  com = 0;
    long long n = s.size();
    cerr << "{";
    for (T i : s) {
        if (com != 0) {
            cerr << ",";
        }
        com++;
        _print(i);
    }
    cerr << "}";
}


template<class T, class V> void _print(map<T, V>mp) {

    cerr << "[";
    cerr << "\n";
    int start = 0;
    for (auto it = mp.begin(); it != mp.end(); it++) {
        if (start != 0) {
            cerr << ",";
        }
        cerr << "{";
        _print(it->first);
        cerr << ",";
        _print(it->second);
        cerr << "}";
        cerr << "\n";
    }
    cerr << "]";
}

My template you can add to it according to the requirement.

2 Likes