一个代码极长但是十分全面同时速度很快的快读快写
ExplodingKonjac · · 个人记录
快读 & 快写
与云剪贴板同步
\textbf{Introduction}
-
定义了类
FastInput,FastOutput以及它们的实例qin,qout,分别用于读入和输出; -
FastInput定义了operator>>,FastOutput定义了operator<<,使用方法和cin,cout类似。目前支持读入/输出整数和字符串(std::string,char[],char*); -
FastInput::read(T &...args):对参数顺次调用operator>>; -
FastInput::read(Iter begin,Iter end):对迭代器范围\mathrm{[begin,end)} 顺次调用operator>>; -
FastOutput::writesp(const T &...args):对参数顺次调用operator<<,并用空格隔开; -
FastOutput::writeln(const T &...args):对参数顺次调用operator<<,并用换行隔开; -
FastOutput::writesp(const T &...args):对迭代器范围\mathrm{[begin,end)} 顺次调用operator<<,并用空格隔开; -
FastOutput::writeln(const T &...args):对迭代器范围\mathrm{[begin,end)} 顺次调用operator<<,并用换行隔开; -
读入输出均有缓存功能,成员函数
flush()作用是清空缓存,qin在flush后会失去原有的信息,正常情况请不要使用; -
你可以自己定义一个快速读写类,类构造函数的参数是文件流指针,如
FastInput fin(fopen("1.in","r"))。
\textbf{Tips}
-
若要开启读写缓存,请在代码开头加上
#define OPENIOBUF; -
调试时建议关掉输入的缓存;
-
开启缓存后不要与其他 IO 方式混用。
\textbf{Updates}
-
2021/9/2:添加了单字符的读入和字符串(
char[]、char*、std::string)的输出; -
2021/9/4:添加了字符串(
char[]、char*、std::string)的读入; -
2021/9/10:稍微更改了多参数函数,使其运行更快(?);
-
2021/10/7:改对了
std::string输入的锅; -
2022/1/1:轻微更改代码,挖个浮点数 io 的坑。
-
2022/3/11:大幅度更新。
qin支持像std::cin一样判断是否达到文件尾(使用qin.eof()或(bool)qin),但是由于判断eof,速度比原来略慢;不再需要在代码结束qout.flush(),析构函数会做这件事情;更改了std::string读入方式,修复了隐藏的一堆锅;仍然不能输入浮点数; -
2022/3/13:做了一些小更改;
-
2022/4/11:做了一些小更改;
-
2022/7/15:因为判文件尾不常用就去掉了,回归服务 OI 的初心。
-
2022/7/20:修字符串的锅;
-
2023/5/19:小改动;
-
2023/5/24:加回了 EOF 判定,仍然使用(
qin.eof()和(bool)qin),但是效率甚至更高;加入了范围读入/输出的功能。
\textbf{Code}
#include <bits/stdc++.h>
using namespace std;
// #define OPENIOBUF
namespace FastIO
{
class FastIOBase
{
protected:
#ifdef OPENIOBUF
static const int BUFSIZE=1<<16;
char buf[BUFSIZE+1];
int buf_p=0;
#endif
FILE *target;
FastIOBase(FILE *f): target(f){}
~FastIOBase()=default;
public:
#ifdef OPENIOBUF
virtual void flush()=0;
#endif
};
class FastOutput final: public FastIOBase
{
public:
FastOutput(FILE *f=stdout): FastIOBase(f) {}
#ifdef OPENIOBUF
~FastOutput() { flush(); }
void flush() { fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
void put(char x)
{
#ifdef OPENIOBUF
if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
putc(x,target);
#endif
}
FastOutput &operator <<(char x)
{ return put(x),*this; }
FastOutput &operator <<(const char *s)
{ for(;*s;put(*(s++)));return *this; }
FastOutput &operator <<(const std::string &s)
{ return (*this)<<s.c_str(); }
template<typename T>
std::enable_if_t<std::is_integral<T>::value,FastOutput&> operator <<(T x)
{
if(x<0) return put('-'),(*this)<<(-x);
char stk[std::numeric_limits<T>::digits10+1],*top=stk;
do *(top++)=x%10+'0',x/=10; while(x);
while(top!=stk) put(*(--top));
return *this;
}
template<typename ...T>
void writesp(T &&...x)
{ std::initializer_list<int>{((*this)<<(x),put(' '),0)...}; }
template<typename ...T>
void writeln(T &&...x)
{ std::initializer_list<int>{((*this)<<(x),put('\n'),0)...}; }
template<typename Iter>
std::enable_if_t<std::is_base_of<
std::forward_iterator_tag,
typename std::iterator_traits<Iter>::iterator_category>
::value> writesp(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<' '; }
template<typename Iter>
std::enable_if_t<std::is_base_of<
std::forward_iterator_tag,
typename std::iterator_traits<Iter>::iterator_category>
::value> writeln(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;
class FastInput final: public FastIOBase
{
private:
bool __eof;
public:
FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
{ buf_p=BUFSIZE; }
void flush() { buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
bool eof()const { return buf[buf_p]==EOF; }
#else
{}
bool eof()const { return feof(target); }
#endif
char get()
{
if(__eof) return EOF;
#ifdef OPENIOBUF
if(buf_p==BUFSIZE) flush();
char ch=buf[buf_p++];
#else
char ch=getc(target);
#endif
return ~ch?ch:(__eof=true,EOF);
}
void unget(char c)
{
__eof=false;
#ifdef OPENIOBUF
buf[--buf_p]=c;
#else
ungetc(c,target);
#endif
}
explicit operator bool()const { return !__eof; }
FastInput &operator >>(char &x)
{ while(isspace(x=get()));return *this; }
template<typename T>
std::enable_if_t<std::is_integral<T>::value,FastInput&> operator >>(T &x)
{
char ch,sym=0;x=0;
while(isspace(ch=get()));
if(__eof) return *this;
if(ch=='-') sym=1,ch=get();
for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=get());
return unget(ch),sym?x=-x:x,*this;
}
FastInput &operator >>(char *s)
{
while(isspace(*s=get()));
if(__eof) return *this;
for(;!isspace(*s) && !__eof;*(++s)=get());
return unget(*s),*s='\0',*this;
}
FastInput &operator >>(std::string &s)
{
char str_buf[(1<<8)+1]={0},*p=str_buf;
char *const buf_end=str_buf+(1<<8);
while(isspace(*p=get()));
if(__eof) return *this;
for(s.clear(),p++;;p=str_buf)
{
for(;p!=buf_end && !isspace(*p=get()) && !__eof;p++);
if(p!=buf_end) break;
s.append(str_buf);
}
unget(*p),*p='\0',s.append(str_buf);
return *this;
}
template<typename ...T>
void read(T &&...x)
{ std::initializer_list<int>{((*this)>>(x),0)...}; }
template<typename Iter>
std::enable_if_t<std::is_base_of<
std::forward_iterator_tag,
typename std::iterator_traits<Iter>::iterator_category>
::value> read(Iter begin,Iter end)
{ while(begin!=end) (*this)>>*(begin++); }
}qin;
} // namespace FastIO
using FastIO::qin,FastIO::qout;