zenny0212's blog

By zenny0212, history, 11 months ago, In English

Tired of typing cout more than actual logic? Try this macro. It lets you debug anything (just override << first).

Works for almost every C++ type. Side effects may include cleaner code and sudden confidence in debugging.

//*** debug(x) ***//
#define DEBUG 1

#if DEBUG
#define del cout << '\n'
#define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__)
template <class X, class Y>
ostream& operator<<(ostream& os, pair<X, Y> const& p) {
    return os << "(" << p.first << ", " << p.second << ")";
}
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>& os, Container const& x) {
    int i = 0, n = (int)distance(x.begin(), x.end());
    os << "{ ";
    for (const auto& y : x) os << y << (++i < n ? ", " : "");
    return os << " }";
}
template <typename... Args>
void _debug(const char* s, Args&&... args) {
    cout << "{ ";
    size_t i = 0, cnt = 0, n = sizeof...(args);
    auto next = [&]() {
        while (s[i] && (s[i] == ' ' || s[i] == ',')) ++i;
        size_t st = i;
        while (s[i] && s[i] != ',') ++i;
        return string(s + st, i - st);
    };
    auto dummy = {(cout << next() << ": " << args << (++cnt < n ? ", " : ""), 0)...};
    (void)dummy;
    cout << " }" << '\n';
}
#else
#define del
#define debug(...)
#endif
Full debug code
Output of full debug code

Drop a comment if you agree—or if you’ve got a genius idea you’re hiding!

Update: It works with C++11 or newer C++ versions

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

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by zenny0212 (previous revision, new revision, compare).

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by zenny0212 (previous revision, new revision, compare).

»
11 months ago, hide # |
Rev. 3  
Vote: I like it +1 Vote: I do not like it

Damn, this is good. I used something like this (the code below) (but it messes up on vector<multiset<T>>, vector<set<T>>, etc., type structures). But I guess this doesn't have that problem.

#ifndef ONLINE_JUDGE
vector<string> DBGc{ "\033[0m", "\033[94m", "\033[36m", "\033[92m" }; //debugging colors
template<typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& p) {
    return os << "(" << p.first << ", " << p.second << ")";
}  template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "["; for (auto it = v.begin(); it != v.end(); it++) os << (it == v.begin() ? "" : ", ") << *it; return os << "]";
}  template<typename T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << "{"; for (auto it = s.begin(); it != s.end(); it++) os << (it == s.begin() ? "" : ", ") << *it; return os << "}";
}  template<typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << "{"; for (auto it = s.begin(); it != s.end(); it++) os << (it == s.begin() ? "" : ", ") << *it; return os << "}";
} template<typename A, typename B> ostream& operator<<(ostream& os, const map<A, B>& m) {
    os << "{"; for (auto it = m.begin(); it != m.end(); it++) os << (it == m.begin() ? "" : ", ") << *it; return os << "}";
} void dbg_out() { cout << DBGc[0] << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cout << DBGc[3] << H; if(sizeof...(T)) cout << ", "; dbg_out(T...); }
#define dbg(...) cout << endl << DBGc[1] << "[line:" << __LINE__ << "] " << DBGc[2] << #__VA_ARGS__ << " = ", dbg_out(__VA_ARGS__)
#else
#define dbg(...) void(false)
#endif
»
11 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Thanks a lot for this. I replace cout with cerr so now i can have debug separated from my output.

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by zenny0212 (previous revision, new revision, compare).

»
11 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Thanks. This is so helpful. Going to use it

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Thanks a lot.

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by zenny0212 (previous revision, new revision, compare).

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by zenny0212 (previous revision, new revision, compare).

»
5 months ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

check this out.