Debug in c+C++
Разница между en2 и en3, 3 символ(ов) изменены
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.↵

```C++↵
//*** 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 = distance(x.begin(), x.end());↵
    os << "{ ";↵
    for (const auto& y : x) os << y << (++i < n ? ", " : "");↵
    return os << " }";↵
}↵
template <typename... Args>↵
void _debug(const char* names, Args&&... args) {↵
    string_view s(names);↵
    cout << "{ ";↵
    size_t i = 0, cnt = 0, n = sizeof...(args);↵
    auto next = [&]() {↵
        while (i < s.size() && (s[i] == ' ' || s[i] == ',')) ++i;↵
        size_t st = i;↵
        while (i < s.size() && s[i] != ',') ++i;↵
        return s.substr(st, i - st);↵
    };↵
    ((cout << next() << ": " << args << (++cnt < n ? ", " : "")), ...);↵
    cout << " }" << '\n';↵
}↵
#else↵
#define del↵
#define debug(...)↵
#endif↵
```↵

<spoiler summary="Full debug code">↵
```C++↵
#include <bits/stdc++.h>↵

using namespace std;↵

//*** 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 = distance(x.begin(), x.end());↵
    os << "{ ";↵
    for (const auto& y : x) os << y << (++i < n ? ", " : "");↵
    return os << " }";↵
}↵
template <typename... Args>↵
void _debug(const char* names, Args&&... args) {↵
    string_view s(names);↵
    cout << "{ ";↵
    size_t i = 0, cnt = 0, n = sizeof...(args);↵
    auto next = [&]() {↵
        while (i < s.size() && (s[i] == ' ' || s[i] == ',')) ++i;↵
        size_t st = i;↵
        while (i < s.size() && s[i] != ',') ++i;↵
        return s.substr(st, i - st);↵
    };↵
    ((cout << next() << ": " << args << (++cnt < n ? ", " : "")), ...);↵
    cout << " }" << '\n';↵
}↵
#else↵
#define del↵
#define debug(...)↵
#endif↵

void solve() {↵
    map<int, int> m{{10, 2}, {11, -2}, {-6, 4}};↵
    float i = 10.019;↵
    string s = "this is a string";↵
    unordered_map<int, int> um{{10, 2}, {11, -2}, {-6, 4}};↵
    vector<string> vs{"heh", "hah"};↵
    vector<vector<int>> vvi{{1, 2}, {5, 6, 8}, {}};↵
    vector<int> vi{1, 3, 5, 7, 8};↵
    vector<char> vc{'a', 'c', 'f'};↵
    vector<pair<int, string>> vpi{↵
        {10, "ten"}, {11, "elevent"}, {-6, "negative six"}, {}, {12, "twelve"}};↵
    set<string> ss{"heh", "hah"};↵
    set<set<int>> ssi{{1, 2}, {5, 6, 8}, {}};↵
    const char* cs = "hah";↵
    char c = 's';↵
    vector<bool> vb = {1, 1, 1, 0};↵

    debug(m);↵
    debug(i);↵
    debug(s);↵
    debug(um);↵
    debug(vs);↵
    debug(vvi);↵
    debug(vi);↵
    debug(vc);↵
    debug(vpi);↵
    debug(ss);↵
    debug(ssi);↵
    debug(cs);↵
    debug(c);↵
    debug(vb);↵
    // you can debug with multiple arguments↵
    debug(m, i, s, um, vs, vvi, vi, vc, vpi, ss, ssi, cs, c, vb);↵
}↵

int32_t main() {↵
    ios_base::sync_with_stdio(false);↵
    cin.tie(nullptr);↵

    solve();↵
    return 0;↵
}↵
```↵
</spoiler>↵

<spoiler summary="Output of full debug code">↵
```text↵
{ m: { (-6, 4), (10, 2), (11, -2) } }↵
{ i: 10.019 }↵
{ s: this is a string }↵
{ um: { (11, -2), (-6, 4), (10, 2) } }↵
{ vs: { heh, hah } }↵
{ vvi: { { 1, 2 }, { 5, 6, 8 }, {  } } }↵
{ vi: { 1, 3, 5, 7, 8 } }↵
{ vc: { a, c, f } }↵
{ vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) } }↵
{ ss: { hah, heh } }↵
{ m: { (-6, 4), (10, 2), (11, -2) } }↵
{ i: 10.019 }↵
{ s: this is a string }↵
{ um: { (11, -2), (-6, 4), (10, 2) } }↵
{ vs: { heh, hah } }↵
{ vvi: { { 1, 2 }, { 5, 6, 8 }, {  } } }↵
{ vi: { 1, 3, 5, 7, 8 } }↵
{ vc: { a, c, f } }↵
{ vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) } }↵
{ ss: { hah, heh } }↵
{ i: 10.019 }↵
{ s: this is a string }↵
{ um: { (11, -2), (-6, 4), (10, 2) } }↵
{ vs: { heh, hah } }↵
{ vvi: { { 1, 2 }, { 5, 6, 8 }, {  } } }↵
{ vi: { 1, 3, 5, 7, 8 } }↵
{ vc: { a, c, f } }↵
{ vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) } }↵
{ ss: { hah, heh } }↵
{ um: { (11, -2), (-6, 4), (10, 2) } }↵
{ vs: { heh, hah } }↵
{ vvi: { { 1, 2 }, { 5, 6, 8 }, {  } } }↵
{ vi: { 1, 3, 5, 7, 8 } }↵
{ vc: { a, c, f } }↵
{ vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) } }↵
{ ss: { hah, heh } }↵
{ vs: { heh, hah } }↵
{ vvi: { { 1, 2 }, { 5, 6, 8 }, {  } } }↵
{ vi: { 1, 3, 5, 7, 8 } }↵
{ vc: { a, c, f } }↵
{ vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) } }↵
{ ss: { hah, heh } }↵
{ vi: { 1, 3, 5, 7, 8 } }↵
{ vc: { a, c, f } }↵
{ vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) } }↵
{ ss: { hah, heh } }↵
{ vc: { a, c, f } }↵
{ vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) } }↵
{ ss: { hah, heh } }↵
{ ssi: { {  }, { 1, 2 }, { 5, 6, 8 } } }↵
{ vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) } }↵
{ ss: { hah, heh } }↵
{ ssi: { {  }, { 1, 2 }, { 5, 6, 8 } } }↵
{ cs: hah }↵
{ ssi: { {  }, { 1, 2 }, { 5, 6, 8 } } }↵
{ cs: hah }↵
{ c: s }↵
{ cs: hah }↵
{ c: s }↵
{ vb: { 1, 1, 1, 0 } }↵
{ c: s }↵
{ vb: { 1, 1, 1, 0 } }↵
{ m: { (-6, 4), (10, 2), (11, -2) }, i: 10.019, s: this is a string, um: { (11, -2), (-6, 4), (10, 2) }, vs: { heh, hah }, vvi: { { 1, 2 }, { 5, 6, 8 }{ vb: { 1, 1, 1, 0 } }↵
{ m: { (-6, 4), (10, 2), (11, -2) }, i: 10.019, s: this is a string, um: { (11, -2), (-6, 4), (10, 2) }, vs: { heh, hah }, vvi: { { 1, 2 }, { 5, 6, 8 }, {  } }, vi: { 1, 3, 5, 7, 8 }, vc: { a, c, f }, vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) }, ss: { hah, heh }, ssi: { { m: { (-6, 4), (10, 2), (11, -2) }, i: 10.019, s: this is a string, um: { (11, -2), (-6, 4), (10, 2) }, vs: { heh, hah }, vvi: { { 1, 2 }, { 5, 6, 8 }, {  } }, vi: { 1, 3, 5, 7, 8 }, vc: { a, c, f }, vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) }, ss: { hah, heh }, ssi: { {  }, { 1, 2 }, { 5, 6, 8 } }, cs: hah, c: s, vb: { 1, 1, 1, 0 } }↵
, {  } }, vi: { 1, 3, 5, 7, 8 }, vc: { a, c, f }, vpi: { (10, ten), (11, elevent), (-6, negative six), (0, ), (12, twelve) }, ss: { hah, heh }, ssi: { {  }, { 1, 2 }, { 5, 6, 8 } }, cs: hah, c: s, vb: { 1, 1, 1, 0 } }↵
{  }, { 1, 2 }, { 5, 6, 8 } }, cs: hah, c: s, vb: { 1, 1, 1, 0 } }↵
```↵
</spoiler>↵

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



История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en7 Английский zenny0212 2025-11-23 17:11:40 53 Tiny change: ' hiding!\n' -> ' hiding!\n\nUpdate: It works with C++11 or newer C++ versions\n'
en6 Английский zenny0212 2025-11-23 17:09:58 214
en5 Английский zenny0212 2025-05-30 09:53:26 12
en4 Английский zenny0212 2025-05-30 05:52:02 3168
en3 Английский zenny0212 2025-05-28 18:30:39 3
en2 Английский zenny0212 2025-05-28 18:28:41 0 (published)
en1 Английский zenny0212 2025-05-28 18:28:12 7023 Initial revision (saved to drafts)