1. 程式人生 > 其它 >快讀快輸模板

快讀快輸模板

使用 fread 和 fwrite(mmap不會寫)。

浮點數輸出精度誤差過大,且實現複雜,就沒寫。

#include <cstdio>
struct IO_Tp {
    bool isdigit(char ch) { return ch >= '0' && ch <= '9'; }
    const static int _I_Buffer_Size = 2 << 22;
    char _I_Buffer[_I_Buffer_Size], *_I_pos = _I_Buffer;
    const static int _O_Buffer_Size = 2 << 22;
    char _O_Buffer[_O_Buffer_Size], *_O_pos = _O_Buffer;
    IO_Tp() { fread(_I_Buffer, 1, _I_Buffer_Size, stdin); }
    ~IO_Tp() { fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout); }
    IO_Tp &operator>>(char *s) {
        int _len = 0;
        while ((*_I_pos != ' ') && (*_I_pos != '\n')) s[_len++] = *_I_pos++;
        return *this;
    }
    IO_Tp &operator>>(int &res) {
        int f = 1;
        while (!isdigit(*_I_pos) && (*_I_pos) != '-') ++_I_pos;
        if (*_I_pos == '-') f = -1, ++_I_pos;
        res = *_I_pos++ - '0';
        while (isdigit(*_I_pos)) res = res * 10 + (*_I_pos++ - '0');
        res *= f;
        return *this;
    }
    IO_Tp &operator>>(long long &res) {
        long long f = 1;
        while (!isdigit(*_I_pos) && (*_I_pos) != '-') ++_I_pos;
        if (*_I_pos == '-') f = -1, ++_I_pos;
        res = *_I_pos++ - '0';
        while (isdigit(*_I_pos)) res = res * 10 + (*_I_pos++ - '0');
        return *this;
    }
    IO_Tp &operator>>(double &res) {
        double f = 1;
        while (!isdigit(*_I_pos) && (*_I_pos) != '-') ++_I_pos;
        if (*_I_pos == '-') f = -1, ++_I_pos;
        res = *_I_pos++ - '0';
        while (isdigit(*_I_pos)) res = res * 10 + (*_I_pos++ - '0');
        double t = 0.1;
        if (*_I_pos++ != '.') {
            res *= f;
            return *this;
        }
        while (isdigit(*_I_pos)) {
            res = res + t * (*_I_pos++ - '0');
            t = t * 0.1;
        }
        res = res * f;
        return *this;
    }
    IO_Tp &operator<<(char _ch) {
        *_O_pos++ = _ch;
        return *this;
    }
    IO_Tp &operator<<(char *_s) {
        int _len = 0;
        while (_s[_len]) *_O_pos++ = _s[_len++];
        return *this;
    }
    IO_Tp &operator<<(int _n) {
        if (_n < 0) *_O_pos++ = '-', _n = -_n;
        static char _buf[20];
        char *_pos = _buf;
        do *_pos++ = '0' + _n % 10;
        while (_n /= 10);
        while (_pos != _buf) *_O_pos++ = *--_pos;
        return *this;
    }
    IO_Tp &operator<<(long long _n) {
        if (_n < 0) *_O_pos++ = '-', _n = -_n;
        static char _buf[20];
        char *_pos = _buf;
        do *_pos++ = '0' + _n % 10;
        while (_n /= 10);
        while (_pos != _buf) *_O_pos++ = *--_pos;
        return *this;
    }
} IO;