首页 > 其他分享 >【模板】快读快输

【模板】快读快输

时间:2023-09-13 14:57:04浏览次数:39  
标签:char ch return 快输 ioch IO 快读 模板 getchar

updated on 2023.9.13

namespace RobinChen{
    char buf[1<<20],*p1=buf,*p2=buf;
    int gc(){
        if(p1==p2) p2=(p1=buf)+fread(buf,1,1<<20,stdin);
        return p1==p2?EOF:*p1++;
    }
    template<class T> T read(){
        T x=0; char ch=gc(); int flg=1;
        for(;ch<'0'||'9'<ch;ch=gc()) if(ch=='-') flg=-1;
        for(;'0'<=ch&&ch<='9';ch=gc()) x=x*10+ch-'0';
        return x*flg;
    }
};
using RobinChen::read;
namespace Crxis{
    char buf[1<<20],*p1=buf,*p2=buf+(1<<20);
    void flush(){fwrite(buf,1,p1-buf,stdout),p1=buf;}
    void pc(char ch){
        if(p1==p2) flush();
        *p1++=ch;
    }
    void write(int x){
        if(x==0) return pc('0'),void();
        if(x<0) pc('-'),x=-x;
        static int stk[40];
        int top=0;
        while(x) stk[++top]=x%10,x/=10;
        for(int i=top;i>=1;i--) pc(stk[i]+'0');
    }
};
using Crxis::pc;
using Crxis::write;
using Crxis::flush;

posted on 2021-02-16 22:40:01 | under 学术 | source

警示后人:不要试图去写过于复杂的快读快输,卡常/CE 会让你崩溃

一行式:

const char endl='\n';namespace IO{const int n=1<<20;char i[n],o[n],*ip=i,*iq=i,*op=o,*oq=o+n;char G(){return ip==iq&&(iq=(ip=i)+std::fread(i,1,n,stdin)),ip==iq?EOF:*ip++;}void P(char c){op==oq&&std::fwrite(op=o,1,n,stdout),*op++=c;}bool D(char c){return '0'<=c&&c<='9';}struct F{~F(){std::fwrite(o,1,op-o,stdout);}}f;struct R{template<class T>R operator>>(T&x){x=0;int f=0,c=G();for(;!D(c);c=G())f|=c=='-';for(;D(c);c=G())x=x*10-'0'+c;if(f)x=-x;return*this;}}cin;struct W{template<class T>W operator<<(T x){if(x<0)x=-x,P('-');if(x>9)*this<<x/10;P(x%10+'0');return*this;}}cout;template<>W W::operator<<(char c){P(c);return*this;}};using IO::cin;using IO::cout;
//

重载运算符式:https://www.luogu.com.cn/paste/7clcx6a0

#include <cstdio>
//#define DEBUG
#define NO_IOSTREAM
namespace FAST_IO{
    static const int 
        _MAX_IO_SIZE=1<<15,
        _MAX_NUMBER_LENGTH=20;
    class IO_chars{
        private: char _I_buf[_MAX_IO_SIZE],*_I_p1,*_I_p2;
        private: char _O_buf[_MAX_IO_SIZE],*_O_p1,*_O_p2;
#ifndef DEBUG
        public: inline IO_chars():_I_p1(_I_buf),_I_p2(_I_buf),_O_p1(_O_buf),_O_p2(_O_buf+_MAX_IO_SIZE){}
        public: inline ~IO_chars(){std::fwrite(_O_buf,1,_O_p1-_O_buf,stdout);}
        public: inline char getchar(){
            if(_I_p1==_I_p2){
                _I_p1=_I_buf;
                _I_p2=_I_p1+std::fread(_I_buf,1,_MAX_IO_SIZE,stdin);
            }
            return _I_p1==_I_p2?EOF:*_I_p1++;
        }
        public:inline void putchar(const char &_O_ch){
            if(_O_p1==_O_p2){
                std::fwrite(_O_buf,1,_MAX_IO_SIZE,stdout);
                _O_p1=_O_buf;
            }
            *_O_p1++=_O_ch;
        }
#else
        public: inline char getchar(){return std::getchar();}
        public:inline void putchar(const char &_O_ch){std::putchar(_O_ch);}
#endif
    } ioch;
    class IO{
        public: inline bool isdigit(const char &ch){return '0'<=ch&&ch<='9';}
        public: inline bool isblank(const char &ch){return ch<=32||ch==127;}
#define def(type) \
        public: inline IO operator>>(type &x){\
            bool f;char ch=ioch.getchar();\
            for(f=0;!isdigit(ch);f|=(ch=='-'),ch=ioch.getchar())\
                if(ch==EOF) throw "Runtime error: No any numbers to read.";\
            for(x=0; isdigit(ch);ch=ioch.getchar())\
                x=(x<<3)+(x<<1)+(ch^'0');\
            return (f)&&(x=~x+1),*this;\
        }
        def(short) def(int) def(unsigned int)
        def(long long) def(unsigned long long)
#undef def
#define def(type) \
        public: inline IO operator<<(type x){\
            short stk[_MAX_NUMBER_LENGTH],cnt=0;\
            if(x<0) x=~x+1,ioch.putchar('-');\
            do stk[cnt++]=x%10; while(x/=10);\
            do ioch.putchar(stk[--cnt]+'0'); while(cnt);\
            return *this;\
        }
        def(short) def(int) def(unsigned int)
        def(long long) def(unsigned long long)
#undef def
        public: inline IO operator>>(char &a){
            char ch=ioch.getchar();
            for(; isblank(ch);ch=ioch.getchar())
                if(ch==EOF) return a=ch,*this;
            return a=ch,*this;
        }
        public: inline IO operator<<(const char &a){
            ioch.putchar(a);
            return *this;
        }
        public: inline IO operator>>(char *a){
            char ch=ioch.getchar(); 
            for(; isblank(ch);ch=ioch.getchar())
                if(ch==EOF) throw "Runtime error: No any chars to read.";
            for(;!isblank(ch);ch=ioch.getchar())
                *a++=ch;
            return *a='\0',*this;
        }
        public: inline IO operator<<(const char *a){
            for(;*a;a++)
                ioch.putchar(*a);
            return *this;
        }
    } io;
#ifdef DEBUG
#undef DEBUG
#endif
#ifdef NO_IOSTREAM
#define cin io
#define cout io
#define endl '\n'
#endif
}
using namespace FAST_IO;
int main(){
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

函数式:

#include <cstdio>
namespace FAST_IO{
    //#define DEBUG
    class IO_chars{
        private:
            static const int _MAX_IO_SIZE=1<<20;
            char _I_buf[_MAX_IO_SIZE],*_I_p1,*_I_p2;
            char _O_buf[_MAX_IO_SIZE],*_O_p1,*_O_p2;
        public:
            #ifndef DEBUG
            IO_chars():_I_p1(_I_buf),_I_p2(_I_buf),_O_p1(_O_buf),_O_p2(_O_buf+_MAX_IO_SIZE){}
            ~IO_chars(){std::fwrite(_O_buf,1,_O_p1-_O_buf,stdout);}
            #endif
            char getchar(){
                #ifdef DEBUG
                return std::getchar();
                #endif
                if(_I_p1==_I_p2){
                    _I_p1=_I_buf;
                    _I_p2=_I_p1+std::fread(_I_buf,1,_MAX_IO_SIZE,stdin);
                }
                return _I_p1==_I_p2?EOF:*_I_p1++;
            }
            void putchar(const char &_O_ch){
                #ifdef DEBUG
                std::putchar(_O_ch);
                return ;
                #endif
                if(_O_p1==_O_p2){
                    std::fwrite(_O_buf,1,_MAX_IO_SIZE,stdout);
                    _O_p1=_O_buf;
                }
                *_O_p1++=_O_ch;
            }
    } IOER;
    class IO{
        private:
            static const int _MAX_LEN=20,_ZERO=(int)'0';
            int stack[_MAX_LEN];
        public:
            bool isdigit(const char &ch){return '0'<=ch&&ch<='9';}
            bool isblank(const char &ch){return ch==' '||ch==EOF||ch=='\n'||ch=='\r'||ch=='\t';}
            template<typename T>
            inline void read(T &x){
                #ifdef DEBUG
                std::scanf("%d",&x);
                return ;
                #endif
                x=0;int f=0;char ch=IOER.getchar();
                for(;!isdigit(ch);f|=(ch=='-'),ch=IOER.getchar())
                    if(ch==EOF) return ;
                for(; isdigit(ch);ch=IOER.getchar())
                    x=(x<<3)+(x<<1)+(ch^_ZERO);
                if(f) x=(~x+1);
            }
            template<typename T>
            inline void write(const T &x){
                #ifdef DEBUG
                std::printf("%d",x);
                return ;
                #endif
                int num=x,top=0;
                if(num<0) num=(~num+1),IOER.putchar('-');
                do stack[top++]=num%10,num/=10; while(num);
                while(top) IOER.putchar(stack[--top]+_ZERO);
            }
            inline void readstr(char *a){
                #ifdef DEBUG
                std::scanf("%s",a);
                return ;
                #endif
                int i=0;char ch=IOER.getchar();
                for(; isblank(ch);ch=IOER.getchar())
                    if(ch==EOF) return ;
                for(;!isblank(ch);ch=IOER.getchar())
                    a[i++]=ch;
                a[i]='\0';
            }
            inline void gets(char *a){
                #ifdef DEBUG
                std::scanf("%[^\n\r]",a);
                //不知道这种方式读入的字符串有没有\0,没有那就换成std::gets吧
                return ;
                #endif
                int i=0;char ch=IOER.getchar();
                for(; (ch==EOF||ch=='\n'||ch=='\r');ch=IOER.getchar())
                    if(ch==EOF) return ;
                for(;!(ch==EOF||ch=='\n'||ch=='\r');ch=IOER.getchar())
                    a[i++]=ch;
                a[i]='\0';
            }
            inline void puts(const char *a){
                #ifdef DEBUG
                std::puts(a);
                return ;
                #endif
                for(int i=0;a[i]!='\0';i++)
                    IOER.putchar(a[i]);
                IOER.putchar('\n');
            }
    } io;
    //#undef DEBUG
}
using FAST_IO::io;
int x;
char a[10010];
int main(){
    io.read(x);
    io.write(x);
    io.puts("");
    io.gets(a);
    io.puts(a);
    return 0;
}

标签:char,ch,return,快输,ioch,IO,快读,模板,getchar
From: https://www.cnblogs.com/caijianhong/p/template-fastio.html

相关文章

  • pycharm设置新建Python文件的模板
    首先找到Pycharm设置默认文件的位置,File-Setting-Editor-FileandCodeTemplates->PythonScript最后附上相应的编写内容大家按需选择:#coding:utf-8——>这里是设置的编码格式,根据自己的实际情况可以修改#当前的项目名:${PROJECT_NAME}#当前编辑文件名:${NAME}#当前......
  • 视图模板____Freemarker入门demo
    //工程结构//代码类packagecom.freemarker.test;importjava.io.File;importjava.io.FileWriter;importjava.io.PrintWriter;importjava.util.HashMap;importjava.util.Map;importfreemarker.template.Configuration;importfreemarker.temp......
  • 自动生成学生成绩分析软件,点击即用,完全自动化模板
    用户界面设计:创建一个用户友好的界面,包括输入成绩数据的功能。提供可选择的数据源选项,如Excel文件、数据库等。允许用户设置分析参数和选项。数据导入:实现数据导入功能,支持从不同来源导入成绩数据。解析导入的数据,并进行格式验证和清洗处理。将清洗后的数据存储到内部数据库或数......
  • 【Spring Boot】Thymeleaf 模板引擎
     Thymeleaf组成:标签+表达式,标签是Thymeleaf的语法结构,而表达式就是语法里的内容实现  pom.xml添加依赖包<!--模板引擎Thymeleaf依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-b......
  • 多项式模板
    总算把之前摸鱼多项式欠下的东西还清了些。。。常数应该不算特别大点击查看代码namespacePolys{#definePolystd::vector<int>#definelllonglongconstintG=3,MOD=998244353;llpower(lla,llb=MOD-2){llret=1;......
  • 使用EasyExcel实现无模板、全自定义Excel导出
    1需求背景最近公司需要做一个动态字段的Excel导出,大致的样式如下:实体类如下://部门实体类publicclassDepartment{privateStringcompanyName;privateStringname;privateStringfullName;privateStringleaderName;privateStringbusiness;......
  • C++模板介绍
    C++模板C++模板是一种强大的泛型编程工具,它允许我们编写通用的代码,可以用于处理多种不同的数据类型。模板允许我们在编写代码时将类型作为参数进行参数化,从而实现代码的重用性和灵活性。在C++中,模板由关键字template开始,并且后面跟着模板参数列表。模板参数可以是类型参数......
  • c++高精度模板
    #include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<string>#include<vector>#include<list>usingnamespacestd;constintmaxn......
  • 最长上升子序列----nlogn算法-模板
    #include<iostream>#include<vector>#defineMAX1010usingnamespacestd;vector<int>len;//这里我返回的满足len[k]>=val[i]且k最小的位置//和上文红色部分的描述是等价的,只是变成了更新len[k],而不是len[k+1]intbisearch(intval){intleft=0,right=len.size(......
  • 高精度加减法模板
    正整数+-法#include<stdio.h>#include<string.h>voidplus(char*a,char*b,char*c);voidminus(char*a,char*b,char*c);voidminus(char*a,char*b,char*c){ inti,j,k; intla=strlen(a); intlb=strlen(b); intflag=0; memset(c,0......