oi 技巧

模板代码

快读快写

class IO {
private:
    static const int MAX_SIZE = 1 << 20;
    char buf[MAX_SIZE], *p1, *p2;
    char pbuf[MAX_SIZE], *pp;

public:
#ifdef ExRoc
#else
    IO() : p1(buf), p2(buf), pp(pbuf) {}

    ~IO() {
        fwrite(pbuf, 1, pp - pbuf, stdout);
    }
#endif

    char getCh() {
#ifdef ExRoc
        return getchar();
#endif
        if (p1 == p2) {
            p1 = buf;
            p2 = p1 + fread(buf, 1, MAX_SIZE, stdin);
        }
        return p1 == p2 ? ' ' : *p1++;
    }

    bool isBlank(char ch) {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
    }

    template <class T>
    void read(T &x) {
        bool sign = 0;
        char ch = getCh();
        while (!isdigit(ch)) {
            if (ch == '-') {
                sign = true;
            }
            ch = getCh();
        }

        x = 0;
        while (isdigit(ch)) {
            x = x * 10 + (ch - '0');
            ch = getCh();
        }

        if (ch == '.') {
            double tmp = 1.0;
            for (ch = getCh(); isdigit(ch); ch = getCh()) {
                tmp /= 10.0;
                x += tmp * (ch - '0');
            }
        }
        if (sign) {
            x = -x;
        }
    }

    void read(char *s) {
        char ch;
        for (ch = getCh(); isBlank(ch); ch = getCh());
        while (!isBlank(ch)) {
            *s++ = ch;
            ch = getCh();
        }
        *s = 0;
    }

    void read(char &c) {
        for (c = getCh(); isBlank(c); c = getCh());
    }

    void putCh(const char &c) {
#ifdef ExRoc
        putchar(c);
#else
        if (pp - pbuf == MAX_SIZE) {
            fwrite(pbuf, 1, MAX_SIZE, stdout);
            pp = pbuf;
        }
        *pp++ = c;
#endif
    }

    template <class T>
    void write(T x) {
        if (x < 0) {
            x = -x;
            putCh('-');
        }
        static T sta[35];
        int top = 0;
        do {
            sta[top++] = x % 10;
            x /= 10;
        } while (x != 0);
        while (top != 0) {
            putCh(sta[--top] + '0');
        }
    }

    template <class T>
    void write(T x, char lastChar) {
        write(x);
        putCh(lastChar);
    }
} io;

推荐题目

参考链接

OI Wiki 读入、输出优化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值