代码拉取完成,页面将自动刷新
#include <bits/stdc++.h>
using namespace std;
inline namespace debug {
/***** declarations ************/
string toString(bool);
string toString(char);
string toString(int);
string toString(float);
string toString(double);
string toString(long long);
string toString(long double);
string toString(const string &);
string toString(const char *);
template<typename E1, typename E2>
string toString(const pair<E1, E2> &);
template<typename E1, typename E2, typename E3>
string toString(const tuple<E1, E2, E3> &);
template<typename E1, typename E2, typename E3, typename E4>
string toString(const tuple<E1, E2, E3, E4> &);
template<typename E1, typename E2, typename E3, typename E4, typename E5>
string toString(const tuple<E1, E2, E3, E4, E5> &t);
template<typename E1, typename E2, typename E3, typename E4, typename E5, typename E6>
string toString(const tuple<E1, E2, E3, E4, E5, E6> &t);
template<typename E>
string toString(vector<E> &, string);
template<typename E>
string toString(vector<E>);
template<typename E>
string toString(queue<E>);
template<class E>
string toString(priority_queue<E>);
template<typename E, typename T, typename C>
string toString(priority_queue<E, T, C> pq);
template<typename E>
string toString(deque<E>);
template<typename E, typename C>
string toString(set<E, C> s);
template<typename E>
string toString(set<E>);
template<typename E>
string toString(unordered_set<E>);
template<typename E, typename C>
string toString(multiset<E, C> s);
template<typename E>
string toString(multiset<E>);
template<typename E>
string toString(unordered_multiset<E>);
template<typename E1, typename E2>
string toString(map<E1, E2>);
template<typename E1, typename E2>
string toString(unordered_map<E1, E2>);
template<typename E1, typename E2>
string toString(multimap<E1, E2>);
template<typename E1, typename E2>
string toString(unordered_multimap<E1, E2>);
/********************** End **************************/
string toString(bool x) {
return x ? "true" : "false";
}
string toString(char x) {
return x == '\n' ? "\n" : ("\'" + string({x}) + "\'");
}
string toString(int x) {
return to_string(x);
}
string toString(float x) {
return to_string(x);
}
string toString(double x) {
return to_string(x);
}
string toString(long long x) {
return to_string(x);
}
string toString(long double x) {
return to_string(x);
}
string toString(const string &x) {
return x == "\n" ? "\n" : ("\"" + x + "\"");
}
string toString(const char *x) {
return toString(string(x));
}
template<typename E1, typename E2>
string toString(const pair<E1, E2> &p) { return "(" + toString(p.first) + "," + toString(p.second) + ")"; }
template<typename E1, typename E2, typename E3>
string toString(const tuple<E1, E2, E3> &t) {
auto[a, b, c] = t;
char *name = new char[100];
sprintf(name, "(%s,%s,%s)", toString(a).c_str(), toString(b).c_str(), toString(c).c_str());
string res(name);
delete[] name;
return res;
}
template<typename E1, typename E2, typename E3, typename E4>
string toString(const tuple<E1, E2, E3, E4> &t) {
auto[a, b, c, d] = t;
char *name = new char[100];
sprintf(name, "(%s,%s,%s,%s)", toString(a).c_str(), toString(b).c_str(), toString(c).c_str(),
toString(c).c_str());
string res(name);
delete[] name;
return res;
}
template<typename E1, typename E2, typename E3, typename E4, typename E5>
string toString(const tuple<E1, E2, E3, E4, E5> &t) {
auto[a, b, c, d, e] = t;
char *name = new char[100];
sprintf(name, "(%s,%s,%s,%s,%s)", toString(a).c_str(), toString(b).c_str(), toString(c).c_str(),
toString(c).c_str(), toString(e).c_str());
string res(name);
delete[] name;
return res;
}
template<typename E1, typename E2, typename E3, typename E4, typename E5, typename E6>
string toString(const tuple<E1, E2, E3, E4, E5, E6> &t) {
auto[a, b, c, d, e, f] = t;
char *name = new char[100];
sprintf(name, "(%s,%s,%s,%s,%s,%s)", toString(a).c_str(), toString(b).c_str(), toString(c).c_str(),
toString(c).c_str(), toString(e).c_str(), toString(f).c_str());
string res(name);
delete[] name;
return res;
}
template<typename E>
string toString(vector<E> &e, string bracket) {
string res;
if (e.empty()) res = bracket;
else {
char lf = bracket[0];
char rt = bracket[1];
res += lf;
auto it = e.begin();
res += toString(*(it++));
while (it != e.end()) {
res += "," + toString(*(it++));
}
res += rt;
}
return res;
}
template<typename E>
string toString(vector<E> e) {
return toString(e, "[]");
}
template<typename E>
string toString(queue<E> q) {
auto &dq = (deque<E> &) q;
vector<E> v(begin(dq), end(dq));
return toString(v, "[]");
}
template<class E>
string toString(priority_queue<E> pq) {
vector<E> v;
while (!pq.empty()) {
v.push_back(pq.top());
pq.pop();
}
return toString(v, "[]");
}
template<typename E, typename T, typename C>
string toString(priority_queue<E, T, C> pq) {
vector<E> v;
while (!pq.empty()) {
v.push_back(pq.top());
pq.pop();
}
return toString(v, "[]");
}
template<typename E>
string toString(deque<E> q) {
vector<E> v;
for (auto &&i : q) {
v.push_back(i);
}
return toString(v, "[]");
}
template<typename E, typename C>
string toString(set<E, C> s) {
vector<E> v;
for (auto &&i : s) {
v.push_back(i);
}
return toString(v, "{}");
}
template<typename E>
string toString(set<E> s) {
vector<E> v;
for (auto &&i : s) {
v.push_back(i);
}
return toString(v, "{}");
}
template<typename E>
string toString(unordered_set<E> s) {
vector<E> v(begin(s), end(s));
return toString(v, "{}");
}
template<typename E, typename C>
string toString(multiset<E, C> s) {
vector<E> v;
for (auto &&i : s) {
v.push_back(i);
}
return toString(v, "{}");
}
template<typename E>
string toString(multiset<E> s) {
vector<E> v;
for (auto &&i : s) {
v.push_back(i);
}
return toString(v, "{}");
}
template<typename E>
string toString(unordered_multiset<E> s) {
vector<E> v;
for (auto &&i : s) {
v.push_back(i);
}
return toString(v, "{}");
}
template<typename E1, typename E2>
string toString(map<E1, E2> mp) {
if (mp.empty()) return "{}";
string res = "{";
auto it = mp.begin();
res += "(" + toString(it->first) + "->" + toString(it->second) + ")";
++it;
while (it != mp.end()) {
res += ",(" + toString(it->first) + "->" + toString(it->second) + ")";
++it;
}
res += "}";
return res;
}
template<typename E1, typename E2>
string toString(unordered_map<E1, E2> mp) {
if (mp.empty()) return "{}";
string res = "{";
auto it = mp.begin();
res += "(" + toString(it->first) + "->" + toString(it->second) + ")";
++it;
while (it != mp.end()) {
res += ",(" + toString(it->first) + "->" + toString(it->second) + ")";
++it;
}
res += "}";
return res;
}
template<typename E1, typename E2>
string toString(multimap<E1, E2> mp) {
if (mp.empty()) return "{}";
string res = "{";
auto it = mp.begin();
res += "(" + toString(it->first) + "->" + toString(it->second) + ")";
++it;
while (it != mp.end()) {
res += ",(" + toString(it->first) + "->" + toString(it->second) + ")";
++it;
}
res += "}";
return res;
}
template<typename E1, typename E2>
string toString(unordered_multimap<E1, E2> mp) {
if (mp.empty()) return "{}";
string res = "{";
auto it = mp.begin();
res += "(" + toString(it->first) + "->" + toString(it->second) + ")";
++it;
while (it != mp.end()) {
res += ",(" + toString(it->first) + "->" + toString(it->second) + ")";
++it;
}
res += "}";
return res;
}
// return 0 if c is not separator
// else return -1 if c is left bracket,
// else return 1
int judgeSeparator(char c) {
static const string sep = "(){}[]";
int idx = (int) sep.find(c);
return idx == -1 ? 0 : ((idx & 1) == 0 ? -1 : 1);
}
bool matchSeparator(char pre, char nxt) {
static const string sep[] = {
"()",
"{}",
"[]"
};
string concat({pre, nxt});
return any_of(begin(sep), end(sep), [&](const string &s) { return s == concat; });
}
string operator*(const string &s, int x) {
string res;
for (int i = 0; i < x; i++) {
res.append(s);
}
return res;
}
string concat(vector<string> &v) {
string res;
for (auto &s : v) res += s;
return res;
}
const string tab = " "; /* NOLINT */
string s_format(string _string) {
if (_string.empty()) return "";
if (judgeSeparator(_string[0]) != -1) return _string;
string sepStack;
vector<bool> insideJudgementStack;
vector<vector<string>> data{{""}};
int n = (int) size(_string);
for (int i = 0; i < n; i++) {
char c = _string[i];
if (judgeSeparator(c) == 0) { // 非括号分隔符
data.back().back().push_back(c);
}
else if (judgeSeparator(c) == -1) { // 左括号: ({[
if (!data.empty() and
data.back().back().find("->") == (int) data.back().back().size() - 2) { // map特化,需要为->之后添加\n
string s = tab * (int) size(sepStack) + data.back().back() + "\n";
data.back().pop_back();
data.back().push_back(s);
}
sepStack.push_back(c);
if (!insideJudgementStack.empty()) {
insideJudgementStack.back() = false;
}
insideJudgementStack.push_back(true);
data.emplace_back();
data.back().emplace_back();
}
else if (judgeSeparator(c) == 1) { // 右括号: )}]
bool inside = insideJudgementStack.back(); // 是否是最内层的数据
bool split = i + 1 < n and _string[i + 1] == ','; // 是否需要逗号分隔
string space = tab * ((int) size(sepStack) - 1);
string left = space + sepStack.back() + (inside ? "" : "\n");
string chars = concat(data.back());
string right = (inside ? "" : space) + c + (split ? "," : "") + "\n";
data.pop_back();
data.back().push_back(string().append(left).append(chars).append(right));
sepStack.pop_back();
insideJudgementStack.pop_back();
if (split) i++;
}
}
return data.back().back();
}
void _enter(int x = 1) {
for (int i = 0; i < x; i++) {
puts("");
}
}
template<class E>
void print(const char *name, E e, int enter = 0) {
string nameStr(name);
auto sepIdx = nameStr.find_last_of(',');
if (sepIdx != string::npos) {
nameStr = nameStr.substr(0, sepIdx);
}
string dataStr = s_format(toString(e));
if (!dataStr.empty() && dataStr.back() != '\n') dataStr.push_back('\n');
// 如果只有一个换行,且该换行位于最后,说明是单个嵌套的类型或者单个单词,比如: v=[[0,0,1]], s="abc" 这样,v=和s=之后不换行
int enterIdx = (int) dataStr.find('\n');
bool enterAfterName = (enterIdx != -1) and (enterIdx != (int) size(dataStr) - 1);
printf("%s=%s", nameStr.c_str(), enterAfterName ? "\n" : "");
printf("%s", dataStr.c_str());
_enter(enter);
}
string to_radix_str(long long x, int radix, int width=0) {
// num x in radix 10, change to the param radix
static char _c[65];
auto [end_buf, _] = to_chars(_c, end(_c), x, radix);
string res(_c, end_buf - _c);
if (res.size() < width) res = string(width - res.size(), '0') + res;
return res;
}
string to_bit(long long x, int wide = 0) {
return to_radix_str(x, 2, wide);
}
void print_bit(long long x, int wide = 5) {
printf("%s\n", to_bit(x, wide).c_str());
}
}
#define DEBUG_
#ifdef DEBUG_
#define debug(...) printf(__VA_ARGS__)
#define print(...) print(#__VA_ARGS__, __VA_ARGS__)
#define enter(x) _enter(x)
#define printBit(...) print_bit(__VA_ARGS__)
#else
#define print(...) 42
#define enter(x) 42
#define debug(...) 42
#define printBit(...) 42
#endif
/************************ FOR TEST *******************************/
int32_t main() {
int x = 10;
print(x);
typedef tuple<int, int, int> ti3;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
/**
* test for queue
*/
deque<pii> dq1{{1, 2},
{2, 3}};
queue<pii> q{dq1};
deque<vs> dq2{
{"sss", "abc"},
{"0", "1"}
};
priority_queue<vs> pq;
for (auto &i : dq2) pq.push(i);
print(dq1);
print(q);
print(dq2);
print(pq);
/**
* test for set & map
*/
set<int> st1 = {1, 2, 3, -9, -12, 0};
set<int> st2 = {2, 99, 999};
map<string, set<int>> mp1 = {
{"abcd", st1},
{"eeee", st2},
{"xxxx", {}}
};
map<string, set<int>> mp2 = {
{"1234", st1},
{"9987", st2}
};
vector<map<string, set<int>>> vset = {
mp1, mp2
};
multiset<char> ms = {'a', 'b', 'd', 'd', 'e', 'a'};
unordered_map<char, multiset<char>> um = {{'x', ms},
{'c', {'a', 'p'}}};
print(mp1);
print(mp2);
print(vset);
print(ms);
print(um);
/**
* test for vector
*/
vector<pii> vpii = {{-1, -1},
{2, 0},
{99, 1010}};
vector<ti3> vti3 = {{-1, -1, -1},
{0, 0, 9},
{3, 8, 7}};
print(vpii);
print(vti3);
vector<set<int>> v1 = {
st1, st2
};
print(v1, 1);
vector<int> vv1 = {1, 2, 3, 4, -1, -8};
vector<int> vv2 = {0, 0, 3, 5};
vector<vector<int>> v = {vv1, vv2};
print(v, 2);
vector<vector<vector<int>>> vvv = {
v,
{vv1, vv1},
{vv2, vv2}
};
print(vvv);
debug("%s %d %d %d\n", "ok", 1, 2, 3);
printBit(5);
printBit(7, 10);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。