【模板】快读快写

快读

  • 普通
inline int read(){
	int x=0;bool f=1;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=0;s=getchar();}
	while(s>='0'&&s<='9'){x=(x<<1)+(x<<3)+(s^48);s=getchar();}
	return f?x:-x;	
}

inline double fread() {//浮点数
    double x = 0, y = 1.0;
    char c = getchar();
    while (c<'0'||c>'9') {
        if (c == '-') y = -1.0;
        c = getchar();
    }
    while (c>='0'&&c<='9' || c == '.') {
        if (c>='0'&&c<='9') x = x * 10 + c - '0';
        else {
            double t = 0.1;
            c = getchar();
            while (isdigit(c)) {
                x += (c - '0') * t;
                t *= 0.1;
                c = getchar();
            }
            break;
        }
        c = getchar();
    }
    return x * y;
}
  • 略快
#include <bits/stdc++.h>
using namespace std;

namespace IO{
    #define re register
    #define getchar() (p1==p2&&(p2=(p1=ibuf)+fread(ibuf,1,buf_size,stdin),p1==p2)?EOF:*p1++)
    #define putchar(x) ((p3==obuf+buf_size)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
    #define tpl template<typename T>
    #define tpa template<typename T,typename... Args>
    const int buf_size=1<<21;
    char ibuf[buf_size],*p1=ibuf,*p2=ibuf;
    char obuf[buf_size],*p3=obuf;
    tpl inline void read(T &x){
        x=0;re bool ff=false;re char ch=getchar();
        while(ch<'0'||ch>'9'){ff|=(ch=='-');ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
        x=ff?~x+1:x;
    }
    tpa inline void read(T& x,Args&... args){read(x);read(args...);}
    inline void readc(char &c){
        do{c=getchar();}while(!((c>='a'&&c<='z')||(c>='A'&&c<='Z')));
    }
    tpl inline void write(T x){
        static T st[45];T top=0;
        if(x<0)x=~x+1,putchar('-');
        do{st[top++]=x%10;}while(x/=10);
        while(top)putchar(st[--top]^48);
    }
    tpl inline void write(T x,const char c){write(x);putchar(c);}
    inline void writes(const char *c){while(*c)putchar(*c++);putchar('\n');}
}using namespace IO;

signed main(){
    int a,b;read(a);read(b);
    write(a+b);
    fwrite(obuf,1,p3-obuf,stdout);
    return 0;
}
  • linux下可用:
#define getchar() stdin->_IO_read_ptr<stdin->_IO_read_end?*stdin->_IO_read_ptr++:__uflow(stdin)
#define putchar(x) stdout->_IO_write_ptr<stdout->_IO_write_end?*stdout->_IO_write_ptr++=x:__overflow(stdout,x)
  • 更快:
namespace IO{
    #ifdef LOCAL
    FILE*Fin(fopen("test.in","r")),*Fout(fopen("test.out","w"));
    #else
    FILE*Fin(stdin),*Fout(stdout);
    #endif
    class qistream{static const size_t SIZE=1<<16,BLOCK=32;FILE*fp;char buf[SIZE];int p;public:qistream(FILE*_fp=stdin):fp(_fp),p(0){fread(buf+p,1,SIZE-p,fp);}void flush(){memmove(buf,buf+p,SIZE-p),fread(buf+SIZE-p,1,p,fp),p=0;}qistream&operator>>(char&str){str=getch();while(isspace(str))str=getch();return*this;}template<class T>qistream&operator>>(T&x){x=0;p+BLOCK>=SIZE?flush():void();bool flag=false;for(;!isdigit(buf[p]);++p)flag=buf[p]=='-';for(;isdigit(buf[p]);++p)x=x*10+buf[p]-'0';x=flag?-x:x;return*this;}char getch(){return buf[p++];}qistream&operator>>(char*str){char ch=getch();while(ch<=' ')ch=getch();for(int i=0;ch>' ';++i,ch=getch())str[i]=ch;return*this;}}qcin(Fin);
    class qostream{static const size_t SIZE=1<<16,BLOCK=32;FILE*fp;char buf[SIZE];int p;public:qostream(FILE*_fp=stdout):fp(_fp),p(0){}~qostream(){fwrite(buf,1,p,fp);}void flush(){fwrite(buf,1,p,fp),p=0;}template<class T>qostream&operator<<(T x){int len=0;p+BLOCK>=SIZE?flush():void();x<0?(x=-x,buf[p++]='-'):0;do buf[p+len]=x%10+'0',x/=10,++len;while(x);for(int i=0,j=len-1;i<j;++i,--j)swap(buf[p+i],buf[p+j]);p+=len;return*this;}qostream&operator<<(char x){putch(x);return*this;}void putch(char ch){p+BLOCK>=SIZE?flush():void();buf[p++]=ch;}qostream&operator<<(char*str){for(int i=0;str[i];++i)putch(str[i]);return*this;}}qcout(Fout);
}using namespace IO;

终极快读(可惜比赛不能用):

#include <sys/mman.h>//头文件
//read(x) x:要读入的数
const char*mbuf=(char*)mmap(nullptr,1<<28,PROT_READ,MAP_PRIVATE,fileno(stdin),0);template<typename T>inline void read(T&x){x=0;T f=1;while(*mbuf<'0'||*mbuf>'9'){if(*mbuf=='-')f=-1;mbuf++;}while(*mbuf>='0'&&*mbuf<='9')x=x*10+*mbuf++-'0';x*=f;}


快写

inline void write(int x){
	if(x<0) x=-x;
	if(x>9) write(x/10);//递归到最高位,在一层层回溯回来,每次putchar最低位 
	putchar(x%10+'0');
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值