C++ 输入/输入优化

本文介绍了C++标准库中cin/cout的效率问题,并提供了一个名为Qin和Qout的类来优化输入输出操作。通过自定义的read()和write()方法,实现了更快的数据读写速度,同时提供了方便的字符串读取功能。
摘要由CSDN通过智能技术生成

众所周知,C++标准库中cin/cout的效率及其令人捉急,所以今天为大家带来C++的in/out 优化

#include<bits/stdc++.h>
#ifndef ONLINE_JUDGE
#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#endif
#define INF 0x3f3f3f3f
#define N 100005
#define ll long long
#define ull unsigned long long
#define il inline
#define rg register
using namespace std;
class Qin
{
    inline ll read()
    {
        rg ll x(0);
        rg bool w(1);
        rg char ch(0);
        while (!isdigit(ch))
        {
            if (ch == '-')
                w = -1;
            ch = getchar();
        }
        while (isdigit(ch))
        {
            x = (x << 3) + (x << 1) + (ch - '0');
            ch = getchar();
        }
        return x * w;
    }
    bool getline;

public:
    //字符串整行读入开关
    void open_getline(bool f)
    {
        getline = f;
    }
    Qin &operator>>(ll &x)
    {
        x = read();
        return *this;
    }
    Qin &operator>>(int &x)
    {
        x = read();
        return *this;
    }
    Qin &operator>>(char &x)
    {
        x = getchar();
        return *this;
    }
    Qin &operator>>(string &str)
    {
        rg char ch(1);
        while (ch != '\0' && ch != '\n' && (ch != ' ' || getline))
        {
            ch = getchar();
            str.push_back(ch);
        }
        return *this;
    }
    Qin &operator>>(char *str)
    {
        rg int i = 0;
        rg char ch(1);
        while (ch != '\0' && ch != '\n' && (ch != ' ' || getline))
        {
            ch = getchar();
            str[i++] = ch;
        }
        return *this;
    }
} qin;
class Qout
{
    inline void write(int x)
    {
        static int sta[35];
        rg int top(0);
        do
        {
            sta[top++] = x % 10, x /= 10;
        } while (x);
        while (top)
            putchar(sta[--top] + 48); // 48 是 '0'
    }

public:
#define endl '\n'
    inline Qout &operator<<(int x)
    {
        write(x);
        return *this;
    }
    inline Qout &operator<<(char c)
    {
        putchar(c);
        return *this;
    }
    inline Qout &operator<<(const string &str)
    {
        rg int len = str.length();
        for (rg int i = 0; i < len; i++)
            putchar(str[i]);
        return *this;
    }
    inline Qout &operator<<(char *str)
    {
        rg int len = strlen(str);
        for (rg int i = 0; i < len; i++)
            putchar(str[i]);
        return *this;
    }
} qout;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值