c++快读快写

 快读

普通整形读入

int read() {
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c>'9') { if (c == '-') f = -1; c = getchar(); }
    while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
double型快速读入
inline double DoubleRead(){
    long long s = 0, w = 1, k = 0, n = 0, m = 0;
    char ch = getchar(); 
    while(ch < '0' || ch > '9'){
        if(ch == '-') w = -1;
        ch = getchar();
    }
    while((ch >= '0' && ch <= '9') || ch == '.'){
        if (ch == '.')
            n = 1;
        else if (n == 0)
            s = s * 10 + ch - '0';
           else k = k * 10 + ch - '0', m++;
        ch = getchar();
    }
    return (pow(0.1, m) * k + s) * w;
}

快写

输出字符串

inline void writeS(std::string s){
	int len=s.length();
	for(int i=0;i<len;i++)
		putchar(s[i]);
}

整形快输

void write(int x) {
     if(x==0){
         putchar('0');return;
     }
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}

快读快写1.0普通版

inline void read(int &x) {  // 返回类型必须为void,否则竞赛中Linux测评会报错,Windows没事
	x = 0;
	short flag = 1;
	char c = getchar();
    while(c < '0' || c > '9'){
    	// 此处如果只用if的话容易在数据不规范时出错,特别是cin和read混用
        if(c == '-')flag = -1;
        c = getchar();
    }
	while(c >= '0' && c <= '9') {
		x = (x << 3) + (x << 1) + (c ^ 48);  // 48这个数字恰好往后10个数都可以使用位运算,可以写成二进制证明;位运算能用当然更好
		c = getchar();
	}
	x *= flag;
}

inline void readSL(std::string &s){
	char ch=getchar();
	while(ch=='\n') ch=getchar();
	while(ch!='\n') s+=ch,ch=getchar();
}



inline void writeS(std::string s){
	int len=s.length();
	for(int i=0;i<len;i++)
		putchar(s[i]);
}

inline void writeS(std::string s){
	int len=s.length();
	for(int i=0;i<len;i++)
		putchar(s[i]);
}

int main() {
	int a;
	read(a);
}

快读快写2.0升级版

namespace FAST_IO {
#define IN_LEN 250000
#define OUT_LEN 250000
    //以上两个值无需修改
    inline char Getchar() {
        static char buf[IN_LEN], *l = buf, *r = buf;
        if (l == r) r = (l = buf) + fread(buf, 1, IN_LEN, stdin);
        return (l == r) ? EOF : *l++;
    }
    char obuf[OUT_LEN], *ooh = obuf;
    inline void Putchar(char c) {
        if (ooh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf;
        *ooh++ = c;
    }
    inline ll rd() {
        ll x = 0; int ch = Getchar(); bool f = 1;
        while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = Getchar();
        if (ch == '-') { f = 0; ch = Getchar(); }
        while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = Getchar(); }
        return f ? x : -x;
    }
    void write(ll x) { if (x >= 10) write(x / 10), Putchar((char)(x % 10 + '0')); else Putchar((char)(x + '0')); }
    inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }
}
using namespace FAST_IO;

快读快写3.0极限优化板(能够支持几乎所有类型的快读快写模板)

使用命名空间,使用的时候注意变量名的重复,因为string无法直接比较,所以存在了变量里,使用的时候直接调用write和read函数就可以了,但是字符串的话需要put函数和readS函数

#include<bits/stdc++.h>
namespace Std{
	//不能读入char 
//	char buf[1<<20],*p1,*p2;
//	#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1<<20,stdin), p1 == p2) ? 0 : *p1++)
	inline void read128(__int128 &x){
	    bool f=1;x=0;char ch=getchar();
	    while(ch<'0'||ch>'9'){if(ch=='-') f=!f;ch=getchar();}
	    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	    x=(f?x:-x);return;
	}
    inline void write128(__int128 x){
        if(x<0) putchar('-'),x=-x;
        if(x>9) write128(x/10);
        putchar(x%10+'0');return;
    }
    
	inline void readD(double &x){scanf("%lf",&x);}
	
	inline void writeD(double x){printf("%lf",x);}
	
	inline void writeD(double x,int len){printf("%.*lf",len,x);}
	
	template<typename T>inline void readT(T &x){
	    bool f=1;x=0;char ch=getchar();
	    while(ch<'0'||ch>'9'){if(ch=='-') f=!f;ch=getchar();}
	    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	    x=(f?x:-x);return;
	}
	template<typename T>
	inline void writeT(T x){
	    if(x<0) putchar('-'),x=-x;
	    if(x>9) writeT(x/10);
	    putchar(x%10+'0');return;
	}
    
	inline void readS(std::string &s){
		char ch=getchar();
		while(ch==' '||ch=='\n') ch=getchar();
		while(ch!=' '&&ch!='\n') s+=ch,ch=getchar();
	}
	
	inline void readSL(std::string &s){
		char ch=getchar();
		while(ch=='\n') ch=getchar();
		while(ch!='\n') s+=ch,ch=getchar();
	}
	
	inline void writeS(std::string s){
		int len=s.length();
		for(int i=0;i<len;i++)
			putchar(s[i]);
	}
	
	inline int readC(char s[]){
		int tot=0;char ch=getchar();
		while(ch==' '||ch=='\n') ch=getchar();
		while(ch!=' '&&ch!='\n') s[tot++]=ch,ch=getchar();
		return tot;
	}
	inline void writeC(char s[],int len){
		for(int i=0;i<len;i++)
			putchar(s[i]);
	}
	
	inline void readc(char &c){
		c=getchar();
		while(c==' '||c=='\n')
			c=getchar();
	}inline void writec(char c){putchar(c);}
	
	const std::string c="c";
	const std::string i="i";
	const std::string j="j";
	const std::string X="x";
	const std::string y="y";
	const std::string b="b";
	const std::string s="s";
	const std::string Ss="Ss";
	
	template<class T>
	inline void read(T &x) {
		if(typeid(x).name()==i) readT(x);
		else if(typeid(x).name()==j) readT(x);
		else if(typeid(x).name()==X) readT(x);
		else if(typeid(x).name()==y) readT(x);
		if(typeid(x).name()==b){
			int k;readT(k);
			x=(k>0?true:false);
		}else if(typeid(x).name()==s) readT(x);
		else if(typeid(x).name()==c){char ch;readc(ch);x=ch;}
	}
	template<class T,class... Ts>
	inline void read(T &x,Ts&... xx){
		if(typeid(x).name()==i) readT(x);
		else if(typeid(x).name()==j) readT(x);
		else if(typeid(x).name()==X) readT(x);
		else if(typeid(x).name()==y) readT(x);
		if(typeid(x).name()==b){
			int k;readT(k);
			x=(k>0?true:false);
		}else if(typeid(x).name()==s) readT(x);
		else if(typeid(x).name()==c){char ch;readc(ch);x=ch;}
		read(xx...);
	}
	inline void read(std::string &x){readS(x);}
	
	inline void read(char x[]){readC(x);}
	
	inline void read(double &x){readD(x);}
	
	inline void write(double x){writeD(x);}
	
	inline void write(double x,int len){writeD(x,len);}
	
	inline void write(std::string x){writeS(x);}
	
	inline void write(char x[]){writeC(x,strlen(x));}
	
	inline void write(double x,char a){writeD(x);putchar(a);}
	
	inline void write(std::string x,char a){writeS(x);putchar(a);}
	
	inline void write(char x[],char a){writeC(x,strlen(x));putchar(a);}
	
	inline void write(double x,int len,char a){writeD(x,len);putchar(a);}
	
	template<class T>inline void write(T x,char a){
		if(typeid(x).name()==i) writeT(x);
		else if(typeid(x).name()==j) writeT(x);
		else if(typeid(x).name()==X) writeT(x);
		else if(typeid(x).name()==y) writeT(x);
		if(typeid(x).name()==b){
			if(x) writeT(1);
			else writeT(0);
		}else if(typeid(x).name()==s) writeT(x);
		else if(typeid(x).name()==c){writec(x);}
		putchar(a);
	}	
	inline void write(double x,std::string a){writeD(x);writeS(a);}
	
	inline void write(std::string x,std::string a){writeS(x);writeS(a);}
	
	inline void write(char x[],std::string a){writeC(x,strlen(x));writeS(a);}
	
	inline void write(double x,int len,std::string a){writeD(x,len);writeS(a);}
	
	inline void write(char a,std::string s){writec(a);writeS(s);}
	
	template<typename T>inline void write(T x,std::string a){
		if(typeid(x).name()==i) writeT(x);
		else if(typeid(x).name()==j) writeT(x);
		else if(typeid(x).name()==X) writeT(x);
		else if(typeid(x).name()==y) writeT(x);
		if(typeid(x).name()==b){
			if(x) writeT(1);
			else writeT(0);
		}else if(typeid(x).name()==s) writeT(x);
		else if(typeid(x).name()==c){writec(x);}
		writeS(a);
	}
	template<typename T>inline void write(T x){
		if(typeid(x).name()==i) writeT(x);
		else if(typeid(x).name()==j) writeT(x);
		else if(typeid(x).name()==X) writeT(x);
		else if(typeid(x).name()==y) writeT(x);
		if(typeid(x).name()==b){
			if(x) writeT(1);
			else writeT(0);
		}else if(typeid(x).name()==s) writeT(x);
		else if(typeid(x).name()==c) writec(x);
	}
	template<typename T,typename... Ts>
	inline void write(T x,Ts... xx){
		if(typeid(x).name()==i) writeT(x);
		else if(typeid(x).name()==j) writeT(x);
		else if(typeid(x).name()==X) writeT(x);
		else if(typeid(x).name()==y) writeT(x);
		if(typeid(x).name()==b){
			if(x) writeT(1);
			else writeT(0);
		}else if(typeid(x).name()==s) writeT(x);
		else if(typeid(x).name()==c){writec(x);}
		write(xx...);
	}
	inline void put(std::string s){
		int len=s.length();
		for(int i=0;i<len;i++){
			putchar(s[i]);
		}
	}
}
using namespace Std;
using namespace std;
 
signed main(){
	
}

  • 22
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值