快讀快寫模板 Pro Max

jxyanglinus發表於2024-11-23

模板

namespace QuickIO {
    template<typename T> inline void read(T &x) {
        x = 0; signed op = 1; char ch = getchar();
        for (; !isdigit(ch); ch = getchar()) if (ch == '-') op = -1;
        for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
        x *= op;
    }
    template<typename T, typename ...Args> inline void read(T &x, Args &...rest) {
        read(x), read(rest...);
    }
    template<typename T> void write(T x) {
        if (x < 0) putchar('-'), x = -x;
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
    }
    template<> void write<char>(char x) {
        putchar(x);
    }
    template<typename T, typename ...Args> void write(T x, Args ...rest) {
        write(x), write(rest...);
    }
}

使用例

#include <bits/stdc++.h>
using namespace std;

namespace QuickIO {
    template<typename T> inline void read(T &x) {
        x = 0; signed op = 1; char ch = getchar();
        for (; !isdigit(ch); ch = getchar()) if (ch == '-') op = -1;
        for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
        x *= op;
    }
    template<typename T, typename ...Args> inline void read(T &x, Args &...rest) {
        read(x), read(rest...);
    }
    template<typename T> void write(T x) {
        if (x < 0) putchar('-'), x = -x;
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
    }
    template<> void write<char>(char x) {
        putchar(x);
    }
    template<typename T, typename ...Args> void write(T x, Args ...rest) {
        write(x), write(rest...);
    }
}

int main() {
    // 使用例
    int a;
    long long b;
    short c;
    QuickIO::read(a, b, c); // 讀入列表長度可變,型別不限(前提是整數)
    QuickIO::write(a, '+', b, '+', c, '=', a + b + c, '\n'); // 同樣長度可變,並且可以一併輸出單個字元
    return 0;
}

相關文章