【模板】快读快写
快读
- 普通
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'); }
__EOF__

本文作者:GOD_HJ
本文链接:https://www.cnblogs.com/GOD-HJ/p/17366408.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/GOD-HJ/p/17366408.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?