跳到正文

个人记录

模板

WangBX约 7 分钟
原文
文章信息与版本

文章信息

发布
2024年10月4日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 7 分钟
获赞
1

版本

  1. v1当前

整形超级快读模板

C++
namespace io
{
	char *p1,*p2,buf[100001];
	#define getchar() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
	template<typename T>
	inline typename __gnu_cxx::__enable_if<__is_integer<T>::__value,T>::__type read()
	{
		T sum=0;
		char ch;
		do ch=getchar();                               while(!isdigit(ch));
		do sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); while( isdigit(ch));
		return sum;
	}
	template<typename T>
	inline typename __gnu_cxx::__enable_if<__is_integer<T>::__value,void>::__type read(T& sum)
	{
		sum=0;
		char ch;
		do ch=getchar();                               while(!isdigit(ch));
		do sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); while( isdigit(ch));
	}
	#undef getchar
}

上面的那个板子对调用参数进行了判断,确保你不会读入一个 double 之类的玩意儿。在正式考试中,建议使用常规写法:

C++
namespace io
{
	char *p1,*p2,buf[100001];
	#define getchar() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
	template<typename T>
	inline T read()
	{
		T sum=0;
		char ch;
		do ch=getchar();                               while(!isdigit(ch));
		do sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); while( isdigit(ch));
		return sum;
	}
	template<typename T>
	inline void read(T& sum)
	{
		sum=0;
		char ch;
		do ch=getchar();                               while(!isdigit(ch));
		do sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); while( isdigit(ch));
	}
	#undef getchar
}

用法

C++
int n;
io::read(n);

C++
int n=read<int>();
long long m=read<long long>();

超级快写模板

C++
#include<cstdio>
#include<cctype>
#include<cstdlib>
using namespace std;
namespace io_out
{
	char buf[100001];
	char *p1=buf,*p2=buf+100000;
	#define putchar(ch) ((p1==p2?(fwrite(buf,1,100000,stdout),p1=buf):0),*p1++=ch)
	void quit(int ret_val=0){fwrite(buf,1,p1-buf,stdout);exit(ret_val);}
	void print(char ch){putchar(ch);}
	template<class T> void print(T x)
	{
		static char tmp[15];
		int top=0;
		while(x)	tmp[++top]=(x%10)+'0',x/=10;
		if(top==0)	tmp[++top]=0;
		for(int i=top;i>=1;i--)	putchar(tmp[i]);
	}
	void print(const char* str)
	{
		while(*str!=0)	putchar(*str++);
	}
	#undef putchar
}

使用方式:

C++
int main()
{
	srand(time(0));
	if(rand()%2==0)
		print("NO"),quit(0);
	print(123);
	quit();//请在每个程序出口处都调用quit(); 而不是 return 0; 或者 exit(0);
}

时间对比

  • 平台:Windows\text{平台:Windows}
  • 任务:读入 3∗106 个整数 {ai} ,计算 A=⊕{ai+i} 然后根据公式 bi=(bi−1+1)⊕A 生成一个长度为 3×106 序列 b 并将其输出\text{任务:读入 }3*10^6\text{ 个整数 }\left\{a_i\right\}\text{ ,计算 }A=\oplus\left\{a_i+i\right\}\text{ 然后根据公式 }b_i=\left(b_{i-1}+1\right)\oplus A\text{ 生成一个长度为 }3\times 10^6\text{ 序列 }b\text{ 并将其输出}

结果(不开 O2)

cincin.tie(0)ifstreamfastinSfastin
cout4.784s/4.667s5.421s4.554s
cout.tie(0)/2.104s2.108s2.850s1.964s
ofstream2.141s2.060s2.041s2.864s1.980s
fastout1.970s2.023s1.982s2.609s1.751s
Sfastout1.693s1.600s1.604s2.374s1.522s

结果(开 O2)

cincin.tie(0)ifstreamfastinSfastin
cout4.621s/4.467s4.271s4.038s
cout.tie(0)/2.046s1.950s1.707s1.554s
ofstream2.013s1.979s2.022s1.701s1.503s
fastout1.567s1.437s1.484s1.229s1.011s
Sfastout1.361s1.343s1.260s1.029s0.837s

结果(旧版)

代码
scanf/printf            8.903
fastin+printf           6.438
cin/cout                4.992
fastin+fastout          2.958
cin/cout+freopen+sync   1.798
fstream                 1.788
Sfastin+printf          1.761
cin/cout+sync+freopen   1.742
Sfastin+Sfastout        0.5535
原文