알고리즘 문제에 유용한 디버깅 매크로

dohoon·2021년 9월 23일
0

CP & PS

목록 보기
7/8

tourist의 코드를 보면 쉬운 문제는 짧게짧게 쓰지만,
Div.2 E 정도로 어느 정도 난이도 있는 문제에 대해서는 템플릿 코드를 막 쓰는 것을 볼 수 있습니다.

그 템플릿 코드를 분석한 결과, 디버깅에 유용한 매크로를 발견했습니다!
저도 원래 #define debug(x) cout << #x << " is " << x << '\n' 이런 식으로 매크로를 디버깅에 활용해 왔었는데,
여러 개의 변수를 확인하고 싶거나, 배열의 원소들을 전부 확인해보고 싶거나 할 때는 직접 구현했습니다.
그런데 이 템플릿은 여러 인수들을 받아서 그 귀찮음을 해결해줍니다.

Neal Wu의 디버깅 템플릿이랑 비교해보았는데, << operator 오버로딩도 괜찮은 것 같습니다. 다만 tourist꺼가 개인적으로 더 나은 듯.

구현에 대한 질문은 언제든지 환영입니다... tourist보다 한가하거든요 ㅋㅋ

string to_string(const string& s) { return '"'+s+'"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <typename A,typename B> string to_string(pair<A,B> p) { return "("+to_string(p.first)+", "+to_string(p.second)+")"; }
template <typename A,typename B,typename C> string to_string(tuple<A,B,C> p) { return "("+to_string(get<0>(p))+", "+to_string(get<1>(p))+", "+to_string(get<2>(p))+")"; }
template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto& x : v) {
        if (!first) res += ", ";
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}
void debug_out() { cerr << endl; }
template <typename Head,typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#ifdef ONLINE_JUDGE
#define debug(...)
#else
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#endif
profile
이 블로그 관리 안 한지 오래됨 / 백준 dohoon

0개의 댓글