跳到正文

个人记录

快速读入

__youzimo2014__约 4 分钟
原文

这是 2025年9月3日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

这玩意儿输入的话不能和 scanf 和 cin 混用,不然会炸(手写缓冲区)。

输出同理,而且输出换行和空格分别用 endline; 和 putc(' ')

输出完所有东西要 flush()

C++
FILE *inputfile, *outputfile;

namespace IO {
    const int isiz = (1 << 20) + 1, osiz = (1 << 20) + 1; // 如果 MLE 可以改
    int len = 0;
    char ibuf[isiz], outbuf[osiz];
    char *iBegin, *iEnd;
    inline char getc() {
        if (iBegin == iEnd) {
            iBegin = ibuf;
            iEnd = iBegin + fread(ibuf, 1, isiz, inputfile);
            if (iBegin == iEnd) return EOF;
        }
        return *(iBegin++);
    }
#define endline putc('\n')
    inline long long read(){
        char ch = getc();
        long long x = 0ll;
        bool t = 0;
        while (ch < '0' || ch > '9') {
            if (ch == '-') {
                t = true;
            }
            ch = getc();
        }
        while (ch >= '0' && ch <= '9') {
            x *= 10ll;
            x += ch - '0';
            ch = getc();
        }
        return t ? -x : x;
    }
    void readStr(char *s) {
        char ch = getc();
        int len = 0;
        while (ch == ' ' || ch == '\n' || ch == '\r')
			if (ch == EOF) break;
			else ch = getc();
        while (ch != ' ' && ch != '\n' && ch != '\r' && ch != -1) {
            s[len++] = ch;
            ch = getc();
        }
    }
    void flush() {
        fwrite(outbuf, 1, len, outputfile);
        len = 0;
    }
    void putc(char ch) {
        outbuf[len++] = ch;
        if (len >= osiz - 1) flush(); // 冗余
    }
    template<class T>
    void write(T x){
        if (x == 0) {
            putc('0');
            return;
        }
        if (x < 0) {
            putc('-');
            x = -x;
        }
        if (x > 9) {
            write(x / 10);
        }
        putc(x % 10 + '0');
    }
    template<class T>
    void writeln(T x) {
        write(x);
        endline;
    }
    void writeChars(char *s) {
        for (int i = 0; s[i] != '\0'; i++) {
            putc(s[i]);
        }
    }
    void writeString(string s) {
        for (int i = 0; i < s.length(); i++) {
            putc(s[i]);
        }
    }
}
using IO::getc;
using IO::read;
using IO::write;
using IO::flush;
using IO::putc;
using IO::readStr;
using IO::writeString;
using IO::writeChars;