namespace io

更快的输入输出!然而我没有写输出

看别人的 namespace io 都是一大坨,我自己精简了一下。

支持任意整形、字符串读入。(io::read</*想要读的类型*/>()

原理很简单,在进行文件读入时 fread\tt{fread} 一次读入大量文件,具有较快的速度。

由于 fread\tt{fread} 是读文件的,所以在本地比较麻烦,不想用文件读入时可以把 gc\tt{gc} 改为 getchar\tt{getchar},即使用 std\tt{std} 库里的字符读入。

namespace io {
    const int n = 1e6;
    char c, b[n], *i, *j;

    inline char gc() {
        if (i == j) j = (i = b) + fread(b, 1, n, stdin);
        return i == j ? EOF : *i ++;
    }

    template <typename T> inline T read() {
        T x = 0; int f = 0;
        while (!isdigit(c = gc())) f |= c == '-';
        while (isdigit(c)) x = (x << 1) + (x << 3) + (c & 15), c = gc();
        return f ? -x : x;
    }

    template <> inline string read() {
        string s = "";
        while (!isgraph(c = gc()));
        while (isgraph(c)) s += c, c = gc();
        return s;
    }
}

namespace io
https://ybwa.github.io/p/970b0166/
作者
yb
发布于
2021年11月18日
许可协议