C++ FastIO

📅 2024/10/16
🔖 OI
C/C++ C++20 FastIO

FastIO 是一个快速库。

支持基本类型读写,类似于 cincout 用法类似。

虽然相对于原版函数 fread getchar putchar 要快的。

1. 目录解释

fastio.cpp # 测试代码
fastio.hpp # FastIO 库源代码
fastio.beta.hpp # FastIO 库源代码,使用 C++20
fastio.in # 测试输入数据

2. 使用

is >> n;

读取 n

is >> n >> m >> k;

读取 n m k

c = is.get();

读取一个字符到 c

is >> s;

读取一个字符串 s

is.getline(s);

读取一行到 s

is.getline(s, end);

读取字符到 s,一直读取到 end 停止。

is.get(s);

is.get(s, end);

读取字符到 s,一直读取到 end 停止,但 end 保留在流中。

is.ignore();

忽略一行。

is.ignore(end);

忽略字符到 end 停止。

while (is >> n);

一直读取直到末尾。

is >> bin; is >> oct; is >> dec; is >> hex;

按 2, 8, 10, 16 进制读取数。

is >> ws;

忽略前导空格。

is.setbase(n);

base 进制读取数 (2 ≤ base ≤ 36)

is >> reset;

取消前面的所有设置。

os << n;

写入 n

os << n << m << k;

写入 n m k

os.put(c);

写入一个字符 c

os << flush;

刷新流。

os << endl;

写入换行。

os << ends;

写入空格。

os << boolalpha;

设置写入 bool 时用 true false

os << noboolalpha;

设置写入 bool 时用 0 1

os << showpos;

设置写入正数和 0 时前面加 + 符号。

os << noshowpos;

设置写入正数和 0 时前面不加符号。

os << showpoint;

设置写入浮点数时总是添加小数点。

os << noshowpoint;

设置写入浮点数时不添加小数点。

os << fixed;

设置写入浮点数时保持 setprecision 时设置的位数。

os << defaultfloat;

设置写入浮点数时不保持 setprecision 时设置的位数。

os << bin; os << oct; os << dec; os << hex;

按 2, 8, 10, 16 进制写入整数。

os << lowercase;

写入特殊内容时时使用小写。

os << uppercase;

写入特殊内容时时使用大写。

os << showbase;

写入 2, 8, 16 进制时,前面显示 0b 0 0x

os << noshowbase;

写入 2, 8, 16 进制时,不在前面显示 0b 0 0x

os << setbase(n);

base 进制写入数,超出范围默认认为是 10 进制 (2 ≤ base ≤ 36)

os << setw(n);

设置下一次写入宽度为 width,就填补字符(下一次写入重置)。

os << setfill(c);

设置填充字符。

os << left;

设置 setw 填补的字符在左边。

os << right;

设置 setw 填补的字符在右边。

os << setprecision(n);

设置浮点数保留位数,默认保留 3 位。

os << reset;

取消前面的所有设置。

ifstream ifs(s);

创建文件读取流,文件路径为 s,和普通读取流用法相同。

ofstream ofs(s);

创建文件写入流,文件路径为 s,和普通写入流用法相同。

3. 接口

可以用接口来重新运算符。

注意要用 fastio::interface::istreamfastio::interface::ostream 来重载。

以下不是重载 std::tuple 的实例程序。

        using namespace fastio;

        template <typename... TS>
        interface::istream &operator>>(interface::istream &is, std::tuple<TS...> &a) {
            std::apply([&](auto &&...args) { (is >> args), ...; }, a);
            return is;
        }

        template <typename... TS>
        interface::istream &operator>>(interface::istream &is, std::tuple<TS...> &&a) {
            std::apply([&](auto &&...args) { (is >> args), ...; }, a);
            return is;
        }

        template <typename... TS>
        interface::ostream &operator<<(interface::ostream &os, std::tuple<TS...> const &a) {
            std::apply([&](auto &&...args) { (os << args << ' '), ...; }, a);
            return os;
        }