一个还算能用的调试代码

借鉴了 tourist 的思路

namespace std {
// bool
string to_string(bool v) { return v ? "true" : "false"; }
// string
string to_string(const string &v) { return "\"" + v + "\""; }
// pair
template <typename A, typename B>
string to_string(const pair<A, B> &v) {
	return "(" + to_string(v.first) + ", " + to_string(v.second) + ")";
}
// tuple
template <class Tuple, size_t N>
struct TuplePrinter {
	static string __to_string(const Tuple &t) {
		return TuplePrinter<Tuple, N - 1>::__to_string(t) + ", " +
			   to_string(get<N - 1>(t));
	}
};
template <class Tuple>
struct TuplePrinter<Tuple, 1> {
	static string __to_string(const Tuple &t) { return to_string(get<0>(t)); }
};
template <class... Args>
string to_string(const std::tuple<Args...> &t) {
	return "(" + TuplePrinter<decltype(t), sizeof...(Args)>::__to_string(t) +
		   ")";
}
// container
template <typename Container>
string __to_string(const Container &container) {
	string ans;
	bool flag = 0;
	for (const auto &i : container) {
		if (flag)
			ans += ", ";
		else
			flag = 1;
		ans += to_string(i);
	}
	return ans;
}
// vector
template <typename Value>
string to_string(const vector<Value> &container) {
	return "[" + __to_string(container) + "]";
}
// deque
template <typename Value>
string to_string(const deque<Value> &container) {
	return "[" + __to_string(container) + "]";
}
// array
template <typename Value, size_t N>
string to_string(const array<Value, N> &container) {
	return "[" + __to_string(container) + "]";
}
// bitset
template <size_t N>
string to_string(const bitset<N> &container) {
	return "<" + container.to_string() + ">";
}
// list
template <typename Value>
string to_string(const list<Value> &container) {
	return "[" + __to_string(container) + "]";
}
// forward_list
template <typename Value>
string to_string(const forward_list<Value> &container) {
	return "[" + __to_string(container) + "]";
}
// set
template <typename Key, typename Compare>
string to_string(const set<Key, Compare> &container) {
	return "{" + __to_string(container) + "}";
}
template <typename Key, typename Compare>
string to_string(const multiset<Key, Compare> &container) {
	return "{" + __to_string(container) + "}";
}
template <typename Key, typename Hash, typename Equal>
string to_string(const unordered_set<Key, Hash, Equal> &container) {
	return "{" + __to_string(container) + "}";
}
template <typename Key, typename Hash, typename Equal>
string to_string(const unordered_multiset<Key, Hash, Equal> &container) {
	return "{" + __to_string(container) + "}";
}
// map
template <typename Key, typename Value, typename Compare>
string to_string(const map<Key, Value, Compare> &container) {
	return "{" + __to_string(container) + "}";
}
template <typename Key, typename Value, typename Compare>
string to_string(const multimap<Key, Value, Compare> &container) {
	return "{" + __to_string(container) + "}";
}
template <typename Key, typename Value, typename Hash, typename Equal>
string to_string(const unordered_map<Key, Value, Hash, Equal> &container) {
	return "{" + __to_string(container) + "}";
}
template <typename Key, typename Value, typename Hash, typename Equal>
string to_string(const unordered_multimap<Key, Value, Hash, Equal> &container) {
	return "{" + __to_string(container) + "}";
}
// orz
void orz_second() { cerr << endl; }
void orz_first() { cerr << "empty" << endl; }
template <typename Arg, typename... Args>
void orz_second(const Arg &a, Args... x) {
	cerr << ", " << to_string(a);
	orz_second(x...);
}
template <typename Arg, typename... Args>
void orz_first(const Arg &a, Args... x) {
	cerr << to_string(a);
	orz_second(x...);
}
#define qwq [&] { cerr << "qwq" << endl; }()
// #define orz(x) [&]{cerr<<#x": "<<x<<endl;}()
#define orz(x...)           \
	[&] {                   \
		cerr << #x << ": "; \
		orz_first(x);       \
	}()
#define orzarr(a, n)                             \
	[&] {                                        \
		cerr << #a ": ";                         \
		repeat(__, 0, n) cerr << (a)[__] << " "; \
		cerr << endl;                            \
	}()
#define orzeach(a)                           \
	[&] {                                    \
		cerr << #a ": ";                     \
		for (auto __ : a) cerr << __ << " "; \
		cerr << endl;                        \
	}()
#define pause [&] { cerr << "Line " << __LINE__ << " "; system("pause"); }()
}  // namespace std

用 orz(something) 就能输出调试信息

posted @ 2021-01-17 03:16  axiomofchoice  阅读(87)  评论(0编辑  收藏  举报