上次的那个c++快读快写写的有点简陋,今天再写一篇:

快读(快速读取整数):

#include <iostream>
#include <cctype>

template<typename T>
inline void read(T &x) {
    x = 0;
    T f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    x *= f;
}

// 针对不同整数类型的重载
inline void read(int &x) { read<int>(x); }
inline void read(long long &x) { read<long long>(x); }
inline void read(unsigned int &x) { read<unsigned int>(x); }
inline void read(unsigned long long &x) { read<unsigned long long>(x); }

快写(快速输出整数):

template<typename T>
inline void write(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

// 针对不同整数类型的重载
inline void write(int x) { write<int>(x); }
inline void write(long long x) { write<long long>(x); }
inline void write(unsigned int x) { write<unsigned int>(x); }
inline void write(unsigned long long x) { write<unsigned long long>(x); }

附上完整代码:

#include <iostream>
#include <cctype>
#include <cstdio>

// 快读模板
template<typename T>
inline void read(T &x) {
    x = 0;
    T f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    x *= f;
}

// 快写模板
template<typename T>
inline void write(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

// 类型特化
inline void read(int &x) { read<int>(x); }
inline void read(long long &x) { read<long long>(x); }
inline void write(int x) { write<int>(x); }
inline void write(long long x) { write<long long>(x); }

int main() {
    int a;
    long long b;
    
    // 读取整数
    read(a);
    read(b);
    
    // 输出整数
    write(a);
    putchar('\n');
    write(b);
    putchar('\n');
    
    return 0;
}

更高级的版本:

#include <iostream>
#include <cctype>
#include <cstdio>
#include <string>

class FastIO {
private:
    static const int BUFFER_SIZE = 1 << 20;
    char inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
    char *inptr, *outptr;
    
public:
    FastIO() : inptr(inbuf), outptr(outbuf) {
        setvbuf(stdin, inbuf, _IOFBF, BUFFER_SIZE);
        setvbuf(stdout, outbuf, _IOFBF, BUFFER_SIZE);
    }
    
    ~FastIO() {
        fflush(stdout);
    }
    
    template<typename T>
    void read(T &x) {
        x = 0;
        T f = 1;
        char ch = getchar();
        while (!isdigit(ch)) {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (isdigit(ch)) {
            x = x * 10 + (ch - '0');
            ch = getchar();
        }
        x *= f;
    }
    
    void read(char &c) {
        c = getchar();
        while (isspace(c)) c = getchar();
    }
    
    void read(char *s) {
        char ch = getchar();
        while (isspace(ch)) ch = getchar();
        while (!isspace(ch)) {
            *s++ = ch;
            ch = getchar();
        }
        *s = '\0';
    }
    
    void read(std::string &s) {
        s.clear();
        char ch = getchar();
        while (isspace(ch)) ch = getchar();
        while (!isspace(ch)) {
            s += ch;
            ch = getchar();
        }
    }
    
    template<typename T>
    void write(T x) {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
    }
    
    void write(char c) {
        putchar(c);
    }
    
    void write(const char *s) {
        while (*s) putchar(*s++);
    }
    
    void write(const std::string &s) {
        for (char c : s) putchar(c);
    }
    
    template<typename... Args>
    void writeln(Args... args) {
        write(args...);
        putchar('\n');
    }
};

// 使用示例
int main() {
    FastIO io;
    int n;
    long long m;
    std::string s;
    
    io.read(n);
    io.read(m);
    io.read(s);
    
    io.writeln(n);
    io.writeln(m);
    io.writeln(s);
    
    return 0;
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐