跳到正文

个人记录

fastIO

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

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟

版本

  1. v1当前
C++
#ifndef __FASTIO__
#define __FASTIO__
namespace fastio
{
	namespace getchar
	{
		static constexpr int bufsize=1<<20;
		char Buf[bufsize<<1],*p1=Buf,*p2=Buf;
		inline char gc()
		{
			return (p1==p2&&(p2=(p1=Buf)+fread(Buf,1,bufsize,stdin),p1==p2))?EOF:(*p1++);
		}
	}
	using getchar::gc;
	namespace putchar
	{
		static constexpr int bufsize=1<<20;
		char Buf[bufsize<<1],*p=Buf;
		inline void pc(const char &ch)
		{
			if(p-Buf==bufsize)fwrite(Buf,1,bufsize,stdout),p=Buf;
			*p++=ch;
		}
		void flush()
		{
			fwrite(Buf,1,p-Buf,stdout);
			p=Buf;
		}
	}
	using putchar::pc;
	struct flusher
	{
		~flusher()
		{
			putchar::flush();
		}
	};
	flusher Flusher;
	inline bool empty(const char &ch)
	{
		return ch==' '||ch=='\n'||ch=='\r';
	}
	inline void read(char &ch)
	{
		for(ch=gc();empty(ch);ch=gc());
	}
	inline void read(string &S)
	{
		S.clear();
		char ch;
		for(ch=gc();empty(ch);ch=gc());
		for(;!empty(ch)&&ch!=EOF;ch=gc())S+=ch;
	}
#ifdef __SIZEOF_INT128__
	inline void read(__int128 &x)
	{
		bool o=0;__int128 y=0;char ch;
		for(ch=gc();!isdigit(ch);ch=gc())if(ch=='-')o^=1;
		for(;isdigit(ch);ch=gc())y=y*10+ch-48;
		x=o?-y:y;
	}
#endif
	template<typename tp>
	inline typename enable_if<is_integral<tp>::value&&!is_signed<tp>::value&&!is_same<tp,bool>::value,void>::type read(tp &x)
	{
		char ch;
		x=0;
		for(ch=gc();!isdigit(ch);ch=gc());
		for(;isdigit(ch);ch=gc())x=x*10+ch-48;
	}
	template<typename tp>
	inline typename enable_if<is_integral<tp>::value&&is_signed<tp>::value&&!is_same<tp,bool>::value,void>::type read(tp &x)
	{
		bool o=0;tp y=0;char ch;
		for(ch=gc();!isdigit(ch);ch=gc())if(ch=='-')o^=1;
		for(;isdigit(ch);ch=gc())y=y*10+ch-48;
		x=o?-y:y;
	}
	template<typename tp>
	inline typename enable_if<is_same<tp,bool>::value,void>::type read(tp &x)
	{
		int y;
		read(y);
		x=y;
	}
	char Stk[50];
	inline void write(const char &ch)
	{
		pc(ch);
	}
	inline void write(const string &S)
	{
		for(auto i:S)pc(i);
	}
#ifdef __SIZEOF_INT128__
	inline void write(__int128 x)
	{
		if(x==0)return pc(0);
		int top=0;
		if(x<0)pc('-'),x=-x;
		while(x)
		{
			Stk[top++]=x%10+48;
			x/=10;
		}
		while(top)pc(Stk[--top]);
	}
#endif
	template<typename tp>
	inline typename enable_if<is_integral<tp>::value&&!is_signed<tp>::value&&!is_same<tp,bool>::value,void>::type write(tp x)
	{
		if(x==0)return pc(0);
		int top=0;
		while(x)
		{
			Stk[top++]=x%10+48;
			x/=10;
		}
		while(top)pc(Stk[--top]);
	}
	template<typename tp>
	inline typename enable_if<is_integral<tp>::value&&is_signed<tp>::value&&!is_same<tp,bool>::value,void>::type write(tp x)
	{
		if(x==0)return pc(0);
		int top=0;
		if(x<0)pc('-'),x=-x;
		while(x)
		{
			Stk[top++]=x%10+48;
			x/=10;
		}
		while(top)pc(Stk[--top]);
	}
	template<typename tp>
	inline typename enable_if<is_same<tp,bool>::value,void>::type write(const tp &x)
	{
		x?pc('1'):pc('0');
	}
	struct istream
	{
		template<typename tp>
		inline istream &operator>>(tp &x)
		{
			read(x);
			return *this;
		}
	};
	struct ostream
	{
		template<typename tp>
		inline ostream &operator<<(const tp &x)
		{
			write(x);
			return *this;
		}
		void flush()
		{
			putchar::flush();
		}
	};
}
fastio::istream fin;
fastio::ostream fout;
#endif

部分基于 wyzfastio 修改,重载了 << 和 >>。

定义

代码
fastio::istream fin;
fastio::ostream fout;

使用方法类似 cin cout。

原文