Hi everyone.↵
↵
This is my template for debugging in C++. I was inspired by Tourist's [source code](https://mirror.codeforces.com/contest/1110/submission/49588803) and modified it to my style. Here is it:↵
↵
<spoiler summary="Template">↵
~~~~~↵
#include <bits/stdc++.h>↵
↵
using namespace std;↵
↵
void __print(int x) {cerr << x;}↵
void __print(long x) {cerr << x;}↵
void __print(long long x) {cerr << x;}↵
void __print(unsigned x) {cerr << x;}↵
void __print(unsigned long x) {cerr << x;}↵
void __print(unsigned long long x) {cerr << x;}↵
void __print(float x) {cerr << x;}↵
void __print(double x) {cerr << x;}↵
void __print(long double x) {cerr << x;}↵
void __print(char x) {cerr << '\'' << x << '\'';}↵
void __print(const char *x) {cerr << '\"' << x << '\"';}↵
void __print(const string &x) {cerr << '\"' << x << '\"';}↵
void __print(bool x) {cerr << (x ? "true" : "false");}↵
↵
template<typename T, typename V>↵
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}↵
template<typename T>↵
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}↵
void _print() {cerr << "]\n";}↵
template <typename T, typename... V>↵
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}↵
#ifndef ONLINE_JUDGE↵
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)↵
#else↵
#define debug(x...)↵
#endif↵
~~~~~↵
↵
↵
</spoiler>↵
↵
To debug, just type: `debug(x, y, z...)`. It requires C++ 11 or above.↵
↵
It can work with:↵
↵
- Primitive data types: `bool`, `int`, `long long`, `float`, ...↵
↵
- `std::pair`, `std::string`↵
- Collection types: `std::vector`, `std::map`, `std::set`, ...↵
- Expressions↵
↵
It also support multiple arguments.↵
↵
<hr>↵
↵
**How to use**↵
↵
Primitive data types:↵
↵
~~~~~↵
bool a = true;↵
debug(a);↵
↵
int b = 1;↵
float c = 2.5;↵
long long d = LLONG_MAX;↵
char e = 'e';↵
debug(a, b, c, d, e);↵
↵
Output:↵
'[a] = [true]'↵
'[a, b, c, d, e] = [true, 1, 2.5, 9223372036854775807, 'e']'↵
~~~~~↵
<hr>↵
`std::pair` and `std::string`:↵
↵
~~~~~↵
pair<int, int> a = {1, 2};↵
pair<string, bool> b = {"abcd", false};↵
pair<char, float> c = {'x', 0.5};↵
string d = "This is a string";↵
pair<int, pair<int, int> > e = {1, {2, 3}};↵
debug(a, b, c, d, e);↵
↵
Output:↵
'[a, b, c, d, e] = [{1, 2}, {"abcd", false}, {'x', 0.5}, "This is a string", {1, {2, 3}}]'↵
~~~~~↵
↵
Note: You should only debug a pair of simple data types. For example, the debug won't work if one of pair's elements is collection type (`std::vector`, `std::map`, `std::set`...).↵
↵
<hr>↵
Collection types:↵
↵
Basically, the debug works with collections types which you can iterate by `for (auto i: a)`. So the debugger won't work with collection types like `std::queue` or `std::stack`.↵
↵
~~~~↵
vector<int> a = {1, 2, 3, 4};↵
set<int> b = {1, 2, 2, 3, 3, 4, 4, 5};↵
map<string, int> c;↵
c["string 1"] = 1;↵
c["string 2"] = 2;↵
debug(a, b, c);↵
↵
unordered_map<string, int> d;↵
d["string 3"] = 3;↵
d["string 4"] = 4;↵
multiset<int> e = {5, 5, 4, 3, 1, 1, 2};↵
vector<vector<int> > f = {{1, 2, 3}};↵
debug(d, e, f);↵
↵
Output:↵
'[a, b, c] = [{1, 2, 3, 4}, {1, 2, 3, 4, 5}, {{"string 1", 1}, {"string 2", 2}}]'↵
'[d, e, f] = [{{"string 4", 4}, {"string 3", 3}}, {1, 1, 2, 3, 4, 5, 5}, {{1, 2, 3}}]'↵
~~~~↵
↵
Note: I haven't tried the debug with other complex data types nested in collection types.↵
<hr>↵
Expressions:↵
↵
~~~~↵
int a = 1;↵
int b = 2;↵
debug(a + b, a * b, a / b, a - b, a / (float)b, 2019, 2019 - 1);↵
↵
Output:↵
'[a + b, a * b, a / b, a - b, a / (float)b, 2019, 2019 - 1] = [3, 2, 0, -1, 0.5, 2019, 2018]'↵
↵
~~~~↵
<hr>↵
↵
You can use the template and change it's style to what you want. Hope it would help you debug in C++ easier.↵
↵
↵
↵
This is my template for debugging in C++. I was inspired by Tourist's [source code](https://mirror.codeforces.com/contest/1110/submission/49588803) and modified it to my style. Here is it:↵
↵
<spoiler summary="Template">↵
~~~~~↵
#include <bits/stdc++.h>↵
↵
using namespace std;↵
↵
void __print(int x) {cerr << x;}↵
void __print(long x) {cerr << x;}↵
void __print(long long x) {cerr << x;}↵
void __print(unsigned x) {cerr << x;}↵
void __print(unsigned long x) {cerr << x;}↵
void __print(unsigned long long x) {cerr << x;}↵
void __print(float x) {cerr << x;}↵
void __print(double x) {cerr << x;}↵
void __print(long double x) {cerr << x;}↵
void __print(char x) {cerr << '\'' << x << '\'';}↵
void __print(const char *x) {cerr << '\"' << x << '\"';}↵
void __print(const string &x) {cerr << '\"' << x << '\"';}↵
void __print(bool x) {cerr << (x ? "true" : "false");}↵
↵
template<typename T, typename V>↵
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}↵
template<typename T>↵
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}↵
void _print() {cerr << "]\n";}↵
template <typename T, typename... V>↵
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}↵
#ifndef ONLINE_JUDGE↵
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)↵
#else↵
#define debug(x...)↵
#endif↵
~~~~~↵
↵
↵
</spoiler>↵
↵
To debug, just type: `debug(x, y, z...)`. It requires C++ 11 or above.↵
↵
It can work with:↵
↵
- Primitive data types: `bool`, `int`, `long long`, `float`, ...↵
↵
- `std::pair`, `std::string`↵
- Collection types: `std::vector`, `std::map`, `std::set`, ...↵
- Expressions↵
↵
It also support multiple arguments.↵
↵
<hr>↵
↵
**How to use**↵
↵
Primitive data types:↵
↵
~~~~~↵
bool a = true;↵
debug(a);↵
↵
int b = 1;↵
float c = 2.5;↵
long long d = LLONG_MAX;↵
char e = 'e';↵
debug(a, b, c, d, e);↵
↵
Output:↵
'[a] = [true]'↵
'[a, b, c, d, e] = [true, 1, 2.5, 9223372036854775807, 'e']'↵
~~~~~↵
<hr>↵
`std::pair` and `std::string`:↵
↵
~~~~~↵
pair<int, int> a = {1, 2};↵
pair<string, bool> b = {"abcd", false};↵
pair<char, float> c = {'x', 0.5};↵
string d = "This is a string";↵
pair<int, pair<int, int> > e = {1, {2, 3}};↵
debug(a, b, c, d, e);↵
↵
Output:↵
'[a, b, c, d, e] = [{1, 2}, {"abcd", false}, {'x', 0.5}, "This is a string", {1, {2, 3}}]'↵
~~~~~↵
↵
Note: You should only debug a pair of simple data types. For example, the debug won't work if one of pair's elements is collection type (`std::vector`, `std::map`, `std::set`...).↵
↵
<hr>↵
Collection types:↵
↵
Basically, the debug works with collections types which you can iterate by `for (auto i: a)`. So the debugger won't work with collection types like `std::queue` or `std::stack`.↵
↵
~~~~↵
vector<int> a = {1, 2, 3, 4};↵
set<int> b = {1, 2, 2, 3, 3, 4, 4, 5};↵
map<string, int> c;↵
c["string 1"] = 1;↵
c["string 2"] = 2;↵
debug(a, b, c);↵
↵
unordered_map<string, int> d;↵
d["string 3"] = 3;↵
d["string 4"] = 4;↵
multiset<int> e = {5, 5, 4, 3, 1, 1, 2};↵
vector<vector<int> > f = {{1, 2, 3}};↵
debug(d, e, f);↵
↵
Output:↵
'[a, b, c] = [{1, 2, 3, 4}, {1, 2, 3, 4, 5}, {{"string 1", 1}, {"string 2", 2}}]'↵
'[d, e, f] = [{{"string 4", 4}, {"string 3", 3}}, {1, 1, 2, 3, 4, 5, 5}, {{1, 2, 3}}]'↵
~~~~↵
↵
Note: I haven't tried the debug with other complex data types nested in collection types.↵
<hr>↵
Expressions:↵
↵
~~~~↵
int a = 1;↵
int b = 2;↵
debug(a + b, a * b, a / b, a - b, a / (float)b, 2019, 2019 - 1);↵
↵
Output:↵
'[a + b, a * b, a / b, a - b, a / (float)b, 2019, 2019 - 1] = [3, 2, 0, -1, 0.5, 2019, 2018]'↵
↵
~~~~↵
<hr>↵
↵
You can use the template and change it's style to what you want. Hope it would help you debug in C++ easier.↵
↵
↵