Advertisement
peltorator

Быстрый ввод-вывод от burunduk1

Mar 8th, 2019 (edited)
3,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. /** Interface */
  4.  
  5. inline int readChar();
  6. template <class T = int> inline T readInt();
  7. template <class T> inline void writeInt( T x, char end = 0 );
  8. inline void writeChar( int x );
  9. inline void writeWord( const char *s );
  10.  
  11. /** Read */
  12.  
  13. static const int buf_size = 4096;
  14.  
  15. inline int getChar() {
  16.     static char buf[buf_size];
  17.     static int len = 0, pos = 0;
  18.     if (pos == len)
  19.         pos = 0, len = fread(buf, 1, buf_size, stdin);
  20.     if (pos == len)
  21.         return -1;
  22.     return buf[pos++];
  23. }
  24.  
  25. inline int readChar() {
  26.     int c = getChar();
  27.     while (c <= 32)
  28.         c = getChar();
  29.     return c;
  30. }
  31.  
  32. template <class T>
  33. inline T readInt() {
  34.     int s = 1, c = readChar();
  35.     T x = 0;
  36.     if (c == '-')
  37.         s = -1, c = getChar();
  38.     while ('0' <= c && c <= '9')
  39.         x = x * 10 + c - '0', c = getChar();
  40.     return s == 1 ? x : -x;
  41. }
  42.  
  43. /** Write */
  44.  
  45. static int write_pos = 0;
  46. static char write_buf[buf_size];
  47.  
  48. inline void writeChar( int x ) {
  49.     if (write_pos == buf_size)
  50.         fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
  51.     write_buf[write_pos++] = x;
  52. }
  53.  
  54. template <class T>
  55. inline void writeInt( T x, char end ) {
  56.     if (x < 0)
  57.         writeChar('-'), x = -x;
  58.  
  59.     char s[24];
  60.     int n = 0;
  61.     while (x || !n)
  62.         s[n++] = '0' + x % 10, x /= 10;
  63.     while (n--)
  64.         writeChar(s[n]);
  65.     if (end)
  66.         writeChar(end);
  67. }
  68.  
  69. inline void writeWord( const char *s ) {
  70.     while (*s)
  71.         writeChar(*s++);
  72. }
  73.  
  74. struct Flusher {
  75.     ~Flusher() {
  76.         if (write_pos)
  77.             fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
  78.     }
  79. } flusher;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement