快读快写(蒟蒻版)

· · 个人记录

(说明:我是蒟蒻)

快读 简洁版

想必各位OIer们在各种比赛中总是喜欢使用快读快写,而像我这样的蒟蒻啥都不会写......

所以今天,我便放几份代码在这里,方便大家 Ctrl+c

1.cin cout

cin是我们都知道的方法,常见情况下:

cin>>a;
cout<<a;

这样的代码太慢了, 必须紫菜 其实,加上这串神奇小代码:

cin.tie(0);
cout.tie(0);

就可以使这俩 老奶奶 很慢的读入变得和scanf,printf一样快啦!

2.read() write()

read函数是大佬们最爱用的代码,考前来一串,不怕tle

#include<bits/stdc++.h>
//#define r(a) a=read()
using namespace std;
inline 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;
}

就是这串代码。

long long :将函数类型改成long long

string:(懒得解释了)

#include<bits/stdc++.h>
//#define r(a) a=read()
using namespace std;
string read(){
    string str;
    char ss=getchar(); 
    while(ss==' ' || ss=='\n' || ss=='\r'){
        s=getchar();//遇到这些继续往下读;
    }
    while (s !=' ' && s!='\n' && s!='\r'){
        str+=s;
        s=getchar();//遇到不是空格,不是换行,不是回车就读入这个字符;
    }
    return str;
}

updated 1.17:由于P10815的卡常,现将升级代码放这

#include<bits/stdc++.h>
//#define r(a) a=read()
using namespace std;
inline int read(){
    int x=0,f=1;
    char c=getchar_unlocked();
    while(c<'0'||c>'9'){
        if(c=='-')f=-1;
        c=getchar_unlocked();
    }
    while(c>='0'&&c<='9') {
        x=x*10+c-'0';
        c=getchar_unlocked();
    }
    return x*f;
}

tip:

getchar_unlocked()
putchar_unlocked()

可能会在Windows环境中报错,在Linux环境下没问题

wirte:

#include<bits/stdc++.h>
//#define w(a) write(a)
using namespace std;
inline void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9)write(x/10);
    putchar((x%10) | 0x30);
    return ;
}

long long:"int"换成"long long"

最后来个全家桶

#include<bits/stdc++.h>
//#define r(a) a=read()
//#define w(a) write(a)
using namespace std;
inline 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;
}
inline void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9)write(x/10);
    putchar((x%10) | 0x30);
    return ;
}