主页
搜索
最近更新
数据统计
申请密钥
系统公告
1
/
1
请查看完所有公告
终极IO(使用mmap,fwrite_unlocked)
最后更新于 2025-07-31 11:33:14
作者
chenbs
分类
个人记录
复制 Markdown
查看原文
删除文章
更新内容
`static const unsigned int wS=1<<24` 处调成最大输出文件大小 ```cpp #include<bits/stdc++.h> using namespace std; /* chenbs mmap 究极读写,快到起飞 */ #ifdef __linux__ #include<sys/mman.h> #include<sys/stat.h> #endif #if __cplusplus < 201103 #define noexcept throw() #endif class __FASTIO { #ifdef __linux__ char *rB,*rH,*rT; struct stat Fl; public: inline int gc() noexcept { return *++rH; } inline void jump() noexcept { ++rH; } inline void jump2() noexcept { rH+=2; } __FASTIO() noexcept { int fd = fileno(stdin); fstat(fd,&Fl), rT=(rB=(char*)mmap(0,Fl.st_size+1,PROT_READ,MAP_PRIVATE,fd,0))+Fl.st_size, rH=rB-1; } #else static const int rS=1<<20; char rB[rS],*rH,*rT; public: inline int gc() noexcept { if(rH==rT) rT=(rH=rB)+fread(rB,1,rS,stdin); return (rH==rT)?EOF:*rH++; } inline void jump() noexcept { gc(); } inline void jump2() noexcept { gc(), gc(); } #endif inline int read() noexcept { int x,ch,f=1; while((ch=gc())<48) if(ch=='-') f=-1; x=ch^48; while((ch=gc())>47) x=x*10+(ch^48); return x*f; } inline unsigned readu() noexcept { int x,ch; while((ch=gc())<48); x=ch^48; while((ch=gc())>47) x=x*10+(ch^48); return x; } inline int read_q() noexcept { int x,ch; if((ch=gc())=='-') { x=0; while((ch=gc())>47) x=x*10-(ch^48); }else{ x=(ch^48); while((ch=gc())>47) x=x*10+(ch^48); } return x; } inline unsigned readu_q() noexcept { int x,ch; x=gc()^48; while((ch=gc())>47) x=x*10+(ch^48); return x; } inline long long readll() noexcept { long long x; int ch,f=1; while((ch=gc())<48) if(ch=='-') f=-1; x=ch^48; while((ch=gc())>47) x=x*10+(ch^48); return x*f; } inline unsigned long long readllu() noexcept { long long x; int ch; while((ch=gc())<48); x=ch^48; while((ch=gc())>47) x=x*10+(ch^48); return x; } private: static const unsigned int wS=1<<24; unsigned int wtmp[11], *p=wtmp; char wB[wS+3]; public: char* wT=wB; #define __unsigned__base if(!x){*(++wT)=48;return;}for(;x;x/=10)*(++p)=x%10;while(p>wtmp)*(++wT)=(*(p--))|48; #define __signed__base if(!x){*(++wT)=48;return;}if(x<0)*(++wT)='-',x=-x;for(;x;x/=10)*(++p)=x%10;while(p>wtmp)*(++wT)=(*(p--))|48; inline void write(int x) noexcept { __signed__base } inline void writell(long long x) noexcept { __signed__base } inline void writeu(unsigned x) noexcept { __unsigned__base } inline void writellu(unsigned long long x) noexcept { __unsigned__base } inline void val_write(int &x) noexcept { __signed__base } inline void val_writell(long long &x) noexcept { __signed__base } inline void val_writeu(unsigned &x) noexcept { __unsigned__base } inline void val_writellu(unsigned long long &x) noexcept { __unsigned__base } #undef __unsigned__base #undef __signed__base inline void putc(char c) noexcept { *(++wT)=c; } inline void endl() noexcept { *(++wT)=10; } #ifdef __linux__ ~__FASTIO() noexcept { munmap(rB,Fl.st_size+1), fwrite_unlocked(wB+1,1,wT-wB,stdout); } #else ~__FASTIO() noexcept { fwrite(wB+1,1,wT-wB,stdout); } #endif } io; ``` ## 使用教程 ### 整数 IO `io.read` 读入整数 `io.readu` 读入非负整数(比 `io.read` 快一点) `io.readll` 读入 64 位整数 `io.readllu` 读入非负 64 位整数(比 `io.readll` 快一点) `io.write` 输出整数 `io.writeu` 输出非负整数(比 `io.write` 快一点) `io.writell` 输出 64 位整数 `io.writellu` 输出非负 64 位整数(比 `io.writell` 快一点) ### 字符 IO `io.putc` 输出字符 `io.gc` 读入单字符 `io.jump` 跳过单字符 `io.jump2` 跳过双字符(通常用于读取 `\r\n`) ### $\small\color{red}\text{危险函数}$ `io.read_q`、`io.readu_q` 不检测多余的空格与换行,直接读取。请确保两个数字之间有且仅有一个空格或换行,否则会有致命的错误 为保证最快速度,此类不能与 c/c++ 标准输入输出混合使用(即添加了此类,就不能使用 `cin,cout,scanf,print` 了)。 所有函数均有 `inline`,不用担心数据传递时的速度损失。 但若必须要使用**格式化输出**,可以使用 `sprintf`,例如 ```cpp io.wT+=sprintf(io.wT+1,"%05d",123); ```
正在渲染内容...
点赞
0
收藏
0