Post

Template debug đi thi HSG🔥

Template debug đi thi HSG🔥

Template debug này được mình thiết kế dễ học thuộc để đi thi và dễ dàng code nó hỗ trợ thêm stl bằng cách copy and paste.

Tuy nhiên mình vẫn khuyến khích các bạn hiểu cách hoạt động của nó chứ không thuần chép template

Template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//declarations
template<typename T1, typename T2>
void __print(const pair<T1, T2> &p);

template<typename T, typename ...V>
void __print(const vector<T, V...> &v);

template<typename T1, typename T2, typename ...V>
void __print(const map<T1, T2, V...> &m);

//generic fallback
template<typename T>
void __print(const T &t) {cerr << t;}

//definitions
template<typename T1, typename T2>
void __print(const pair<T1, T2> &p) {
    cerr << '(';
    __print(p.fi);
    cerr << ", ";
    __print(p.se);
    cerr << ')';
}

template<typename T, typename ...V>
void __print(const vector<T, V...> &v) {
    cerr << '[';
    FOR(i, 0, sz(v)) {
        if(i) cerr << ", ";
        __print(v[i]);
    }
    cerr << ']';
}

template<typename T1, typename T2, typename ...V>
void __print(const map<T1, T2, V...> &m) {
    cerr << '{';
    bool first = 1;
    for(const auto &x: m) {
        if(!first) cerr << ", ";
        first = 0;
        __print(x);
    }
    cerr << '}';
}

//debug
template<typename T>
void _print(const T &t) {__print(t);}
void _print() {}
template<typename T, typename ...V>
void _print(const T &t, const V &...v) {
  __print(t); 
  if(sizeof...(v)) cerr << ", "; 
  _print(v...); 
}

#ifdef LOCAL
#define deb(...) do {\
    cerr << "[In " <<  __func__ << "(): Line " << __LINE__ << "] [" << #__VA_ARGS__ << "] = [";\
    _print(__VA_ARGS__);\
    cerr << ']' << nl;\
} while(0);
#else
#define deb(...)
#endif

Về “LOCAL”: Trong Codeblocks, Vào Settings->Compiler/Compiler Settings, ở mục #defines nhập LOCAL vào

Testing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int a = 42;
double b = 3.1415;
string s = "Hello";
char c = 'Z';
bool ok = true;

pair<int, string> p = {7, "Seven"};
vector<int> v = {1, 2, 3, 4};
set<string> st = {"apple", "banana", "cherry"};
map<string, int> mp = {{"Alice", 10}, {"Bob", 20}};

vector<pair<int, int>> vp = {{1,2}, {3,4}, {5,6}};
map<int, vector<int>> mv = {{1,{2,3}}, {4,{5,6,7}}};
set<pair<int,int>> sp = {{1,2}, {3,4}};

deb(a);
deb(b);
deb(s);
deb(c);
deb(ok);
deb(p);
deb(v);
deb(st);
deb(mp);
deb(vp);
deb(mv);
deb(sp);

deb(a, b, s, p, v);
This post is licensed under CC BY 4.0 by the author.