快读快写模版
整型
实用版本
比较适合比赛离线环境,虽然也不太好记
template<typename type>
inline void read(type &x){
x=0;bool flag(0);char ch=getchar();
while(!isdigit(ch)) flag=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
flag?x=-x:0;
}
用法: read(变量名);
template<typename type>
inline void write(type x,bool mode=1)//0为空格,1为换行{
x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
do Stack[++top]=x%10,x/=10; while(x);
while(top) putchar(Stack[top--]|48);
mode?putchar('\n'):putchar(' ');
}
用法: write(x,0)或write(x,1)
0为空格,1为换行
极致优化版本
如果你想直接复制,建议使用这个最快的模板
#include<bits/stdc++.h>
using namespace std;
namespace fio{
const int BS=1<<20;
static char ib[BS],ob[BS];
static char *is=ib,*it=ib,*os=ob;
inline char gc(){
if(is==it){
size_t n=fread(ib,1,BS,stdin);
if(!n) return 0;
is=ib;it=ib+n;
}
return *is++;
}
inline void pc(char c){
if(os==ob+BS){
fwrite(ob,1,BS,stdout);
os=ob;
}
*os++=c;
}
struct Flusher{~Flusher(){if(os!=ob)fwrite(ob,1,os-ob,stdout);}}flusher;
template<class T>
inline void read(T &x){
x=0; int c=gc(); int s=1;
while(c<=32 && c) c=gc();
if(c=='-'){ s=-1; c=gc(); }
while(c>='0'){
x=x*10+(c-'0');
c=gc();
}
if(s<0) x=-x;
}
template<class T>
inline void write(T x,char e='\n'){
if(x<0){ pc('-'); x=-x; }
char s[48]; int n=0;
do{ s[n++]=char('0'+(x%10)); x/=10; }while(x);
while(n--) pc(s[n]);
pc(e);
}
}
using namespace fio;
int main(){
int a,b;
read(a);read(b);
write(a,' ');write(b);
return 0;
}
食用方法
int main(){
int a,b;
read(a); // 快速读入 int
read(b);
write(a,' '); // 快速输出 int,并指定分隔符
write(b); // 默认结尾是 '\n'
return 0;
}
String类型
实用版本
#include<bits/stdc++.h>
using namespace std;
int n;
inline string read(){
string str="";
char cc=getchar();
//处理空格、换行或回车
while(cc==' ' || cc=='\n' || cc=='\r'){
cc=getchar();
}
//读入
while(cc!=' ' && cc!='\n' && cc!='\r'){
str+=cc;
cc=getchar();
}
return str;
}
inline print(string s){
for(int i=0;s[i]!='\0';i++) putchar(s[i]);
}
int main(){
cin>>n;
while(n--){
string s;
s=read();
print(s);
}
return 0;
}
如果你要读一整行,可以试试getline(cin,s)
极致优化版本
#include<bits/stdc++.h>
using namespace std;
namespace fio{
const int S=1<<20;
char ib[S],*i1=ib,*i2=ib,ob[S],*o=ob;
inline char gc(){if(i1==i2){i2=ib+fread(ib,1,S,stdin);i1=ib;if(i1==i2)return 0;}return *i1++;}
inline void pc(char c){if(o==ob+S){fwrite(ob,1,S,stdout);o=ob;}*o++=c;}
struct F{~F(){fwrite(ob,1,o-ob,stdout);}}_;
inline bool read_token(string& s){
s.clear();char c=gc();while(c&&c<=32)c=gc();if(!c)return 0;
while(c>32){s.push_back(c);c=gc();}
return 1;
}
inline bool read_line(string& s){
s.clear();char c=gc();if(!c)return 0;
while(c=='\n'||c=='\r'){c=gc();if(!c)return 0;}
while(c&&c!='\n'&&c!='\r'){s.push_back(c);c=gc();}
if(c=='\r'){char d=gc();if(d&&d!='\n')i1--;}
return 1;
}
inline void write(const string& s){for(char c:s)pc(c);}
inline void nl(){pc('\n');}
}
int main(){
using namespace fio;
string s;
while(read_token(s)){write(s);nl();}
// 若要整行读:把上面一行改为 while(read_line(s)) { ... }
}
食用方法
int main(){
using namespace fio; // 打开命名空间
string s;
// 1. 读以空格/换行分隔的 token
while(read_token(s)){
write(s);
nl();
}
// 2. 读整行(含空格)
// while(read_line(s)){
// write(s);
// nl();
// }
}
char数组类型
因为可以直接cin,且关闭同步等操作后在数据不大的情况下差距不明显,所以不区分版本了
#include<bits/stdc++.h>
using namespace std;
namespace fastio {
const int S=1<<20;
char ib[S],*i1=ib,*i2=ib,ob[S],*o=ob;
inline char gc(){
if(i1==i2){
i2=ib+fread(ib,1,S,stdin);
i1=ib;
if(i1==i2)return 0;
}
return *i1++;
}
inline void pc(char c){
if(o==ob+S){fwrite(ob,1,S,stdout);o=ob;}
*o++=c;
}
struct F{~F(){fwrite(ob,1,o-ob,stdout);}}_;
inline bool read(char *s){
char c=gc();if(!c)return 0;
while(c<=32&&c)c=gc();
if(!c)return 0;
int p=0;
while(c>32){s[p++]=c;c=gc();}
s[p]=0;
return 1;
}
inline void write(const char *s){
while(*s)pc(*s++);
}
inline void nl(){pc('\n');}
}
int main(){
using namespace fastio;
static char s[1000005]; // 开大点,够存一行
while(read(s)){
write(s);
nl();//换行
}
return 0;
}
参考资料
许可协议:
CC BY 4.0