Advertisement
Matrix_code

debug-template

Feb 26th, 2021 (edited)
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5. using ld = long double;
  6. using db = double;
  7. using str = string; // yay python!
  8.  
  9. using pi = pair<int,int>;
  10. using pl = pair<ll,ll>;
  11. using pd = pair<db,db>;
  12.  
  13. using vi = vector<int>;
  14. using vb = vector<bool>;
  15. using vl = vector<ll>;
  16. using vd = vector<db>;
  17. using vs = vector<str>;
  18. using vpi = vector<pi>;
  19. using vpl = vector<pl>;
  20. using vpd = vector<pd>;
  21.  
  22. #define tcT template<class T
  23. #define tcTU tcT, class U
  24. // ^ lol this makes everything look weird but I'll try it
  25. tcT> using V = vector<T>;
  26. tcT, size_t SZ> using AR = array<T,SZ>;
  27. tcT> using PR = pair<T,T>;
  28.  
  29. // pairs
  30. #define mp make_pair
  31. #define f first
  32. #define s second
  33.  
  34. #define sz(x) (int)(x).size()
  35.  
  36. // loops
  37. #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
  38. #define F0R(i,a) FOR(i,0,a)
  39. #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
  40. #define R0F(i,a) ROF(i,0,a)
  41. #define trav(a,x) for (auto& a: x)
  42.  
  43.  // INPUT
  44. #define tcTUU tcT, class ...U
  45. tcT> void re(complex<T>& c);
  46. tcTU> void re(pair<T,U>& p);
  47. tcT> void re(vector<T>& v);
  48. tcT, size_t SZ> void re(AR<T,SZ>& a);
  49.  
  50. tcT> void re(T& x) { cin >> x; }
  51. void re(db& d) { str t; re(t); d = stod(t); }
  52. void re(ld& d) { str t; re(t); d = stold(t); }
  53. tcTUU> void re(T& t, U&... u) { re(t); re(u...); }
  54.  
  55. tcT> void re(complex<T>& c) { T a,b; re(a,b); c = {a,b}; }
  56. tcTU> void re(pair<T,U>& p) { re(p.f,p.s); }
  57. tcT> void re(vector<T>& x) { trav(a,x) re(a); }
  58. tcT, size_t SZ> void re(AR<T,SZ>& x) { trav(a,x) re(a); }
  59. tcT> void rv(int& n, vector<T>& x) { re(n); x.rsz(n); trav(a,x) re(a); }
  60.  
  61. // TO_STRING
  62. #define ts to_string
  63. str ts(char c) { return str(1,c); }
  64. str ts(const char* s) { return (str)s; }
  65. str ts(str s) { return s; }
  66. str ts(bool b) {
  67.     #ifdef LOCAL
  68.         return b ? "true" : "false";
  69.     #else
  70.         return ts((int)b);
  71.     #endif
  72. }
  73. tcT> str ts(complex<T> c) {
  74.     stringstream ss; ss << c; return ss.str(); }
  75. str ts(vector<bool> v) {
  76.     str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]);
  77.     res += "}"; return res; }
  78. template<size_t SZ> str ts(bitset<SZ> b) {
  79.     str res = ""; F0R(i,SZ) res += char('0'+b[i]);
  80.     return res; }
  81. tcTU> str ts(pair<T,U> p);
  82. tcT> str ts(T v) { // containers with begin(), end()
  83.     #ifdef LOCAL
  84.         bool fst = 1; str res = "{";
  85.         for (const auto& x: v) {
  86.             if (!fst) res += ", ";
  87.             fst = 0; res += ts(x);
  88.         }
  89.         res += "}"; return res;
  90.     #else
  91.         bool fst = 1; str res = "";
  92.         for (const auto& x: v) {
  93.             if (!fst) res += " ";
  94.             fst = 0; res += ts(x);
  95.         }
  96.         return res;
  97.  
  98.     #endif
  99. }
  100. tcTU> str ts(pair<T,U> p) {
  101.     #ifdef LOCAL
  102.         return "("+ts(p.f)+", "+ts(p.s)+")";
  103.     #else
  104.         return ts(p.f)+" "+ts(p.s);
  105.     #endif
  106. }
  107.  
  108. // OUTPUT
  109. tcT> void pr(T x) { cout << ts(x); }
  110. tcTUU> void pr(const T& t, const U&... u) {
  111.     pr(t); pr(u...); }
  112. void ps() { pr("\n"); } // print w/ spaces
  113. tcTUU> void ps(const T& t, const U&... u) {
  114.     pr(t); if (sizeof...(u)) pr(" "); ps(u...); }
  115.  
  116.  // DEBUG
  117. void DBG() { cerr << "]" << endl; }
  118. tcTUU> void DBG(const T& t, const U&... u) {
  119.     cerr << ts(t); if (sizeof...(u)) cerr << ", ";
  120.     DBG(u...); }
  121. #ifdef LOCAL // compile with -DLOCAL, chk -> fake assert
  122.     #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  123.     #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
  124.          << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
  125. #else
  126.     #define dbg(...) 0
  127.     #define chk(...) 0
  128. #endif
  129.  
  130. // FILE I/O
  131. void setIn(str s) { freopen(s.c_str(),"r",stdin); }
  132. void setOut(str s) { freopen(s.c_str(),"w",stdout); }
  133. void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
  134. void setIO(str s = "") {
  135.     unsyncIO();
  136.     // cin.exceptions(cin.failbit);
  137.     // throws exception when do smth illegal
  138.     // ex. try to read letter into int
  139.     if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
  140. }
  141. int main() {
  142.     // setIO();
  143.     {
  144.         vector<bool> v = {0,1,1,0,1};
  145.         ps(v);
  146.         bitset<5> b; b[3] = 1;
  147.         ps(b);
  148.     }
  149.     {
  150.         int a = 1;
  151.         ll b = 2;
  152.         db c = 3;
  153.         char d = '4';
  154.         str e = "5";
  155.         bool f = true;
  156.         complex<double> g(1,2);
  157.         ps("OK",a,b,c,d,e,f,g);
  158.     }
  159.     {
  160.         pair<int, int> a = {1, 2};
  161.         pair<string, bool> b = {"abcd", false};
  162.         pair<char, float> c = {'x', 0.5};
  163.         string d = "This is a string";
  164.         pair<int, pair<int, int> > e = {1, {2, 3}};
  165.         dbg(a, b, c, d, e);
  166.         dbg();
  167.     }
  168.     {
  169.         vector<int> a = {1, 2, 3, 4};
  170.         set<int> b = {1, 2, 2, 3, 3, 4, 4, 5};
  171.         map<string, int> c;
  172.         c["string 1"] = 1;
  173.         c["string 2"] = 2;
  174.         dbg(a, b, c);
  175.  
  176.         unordered_map<string, int> d;
  177.         d["string 3"] = 3;
  178.         d["string 4"] = 4;
  179.         multiset<int> e = {5, 5, 4, 3, 1, 1, 2};
  180.         vector<vector<int> > f = {{1, 2, 3}};
  181.         dbg(d, e, f);
  182.     }
  183.     {
  184.         int a = 1;
  185.         int b = 2;
  186.         dbg(a + b, a * b, a / b, a - b, a / (float)b, 2019, 2019 - 1);
  187.     }
  188. }
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement