Advertisement
Chhokmah

C++ Fast iostream

Aug 29th, 2020 (edited)
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. /*
  2.  * Author: chhokmah
  3.  * Date: 2020-08-17 21:42:52
  4.  */
  5. #include <bits/stdc++.h>
  6.  
  7. struct IO {
  8.     const static int size_in = 25 << 20, size_out = 5 << 20;
  9.  
  10.     char in[size_in], *p_in = in;
  11.     char out[size_out], *p_out = out;
  12.  
  13.     IO() {
  14.         freopen("test.in", "r", stdin);
  15.         freopen("test.out", "w", stdout);
  16.         fread(in, 1, size_in, stdin);
  17.     }
  18.  
  19.     ~IO() { fwrite(out, 1, p_out - out, stdout); }
  20.  
  21.     template <typename T>
  22.     T read() {
  23.         T x = 0;
  24.         bool f = 0;
  25.         while (!isdigit(*p_in)) f |= *p_in++ == '-';
  26.         while (isdigit(*p_in)) x = x * 10 + (*p_in++ & 15);
  27.         return f ? -x : x;
  28.     }
  29.  
  30.     IO& operator>>(int& x) { return x = read<int>(), *this; }
  31.     IO& operator>>(long long& x) { return x = read<long long>(), *this; }
  32.     IO& operator>>(unsigned long long& x) { return x = read<unsigned long long>(), *this; }
  33.     IO& operator>>(char& ch) { return ch = *p_in++, *this; }
  34.  
  35.     IO& operator>>(char* s) {
  36.         int n = 0;
  37.         while (*p_in == ' ' || *p_in == '\n' || *p_in == '\0') p_in++;
  38.         while (*p_in != ' ' && *p_in != '\n' && *p_in != '\0') s[n++] = *p_in++;
  39.         return s[n] = '\0', *this;
  40.     }
  41.  
  42.     template <typename T>
  43.     void write(T x) {
  44.         static char buf[20], *p = buf;
  45.         if (x < 0)
  46.             *p_out++ = '-', x = -x;
  47.         do
  48.             *p++ = '0' + x % 10;
  49.         while (x /= 10);
  50.         while (p != buf) *p_out++ = *--p;
  51.     }
  52.  
  53.     IO& operator<<(int x) { return write(x), *this; }
  54.     IO& operator<<(long long x) { return write(x), *this; }
  55.     IO& operator<<(unsigned long long x) { return write(x), *this; }
  56.     IO& operator<<(char ch) { return *p_out++ = ch, *this; }
  57.     IO& operator<<(char* s) {
  58.         for (int i = 0; s[i]; ++i) *p_out++ = s[i];
  59.         return *this;
  60.     }
  61. } io;
  62.  
  63. template <typename T, typename Y>
  64. IO& operator>>(IO& in, std::pair<T, Y>& a) {
  65.     in >> a.first >> a.second;
  66.     return in;
  67. }
  68.  
  69. template <typename T>
  70. IO& operator>>(IO& in, std::vector<T>& a) {
  71.     for (unsigned i = 0; i < a.size(); ++i) in >> a[i];
  72.     return in;
  73. }
  74.  
  75. template <typename T>
  76. IO& operator<<(IO& out, std::vector<T> a) {
  77.     for (unsigned i = 0; i < a.size(); ++i) out << a[i] << ' ';
  78.     return out;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement