zoukankan      html  css  js  c++  java
  • C++实用整数快速输入输出模板(C++)

    随便写一点放在这里,以后想蛇皮卡常就很方便啦
    蒟蒻太懒了,也就暂时不搞什么封namespace之类的操作了
    程序结束时记得flush一下。

    #include<cstdio>
    #define RG register
    #define IV inline void
    #define TP template<typename T>
    #define gc          if(++pi==iend)fread (pi=ibuf,1,SZ,stdin)
    #define pc(C) *po=C;if(++po==oend)fwrite(po=obuf,1,SZ,stdout)
    const int SZ=1<<20;
    char ibuf[SZ],obuf[SZ],*pi=ibuf+SZ-1,*po=obuf;
    const char*iend=ibuf+SZ,*oend=obuf+SZ;
    TP IV in(RG T&x){
    	gc;while(*pi<'-')gc;
    	x=*pi&15;gc;
    	while(*pi>'-'){x*=10;x+=*pi&15;gc;}
    }
    TP IV iex(RG T&x){
    	gc;while(*pi<'-')gc;
    	RG bool f=*pi=='-';if(f)gc;
    	x=*pi&15;gc;
    	while(*pi>'-'){x*=10;x+=*pi&15;gc;}
    	if(f)x=-x;
    }
    TP IV out(RG T x){
    	if(x>9)out(x/10);
    	pc(x%10|'0');
    }
    TP IV oex(RG T x){
    	if(x<0){pc('-');x=-x;}
    	out(x);
    }
    int main(){
    	//do something
    	fwrite(obuf,1,po-obuf,stdout);
    	return 0;
    }
    

    Tips:

    1. gc只是把指针移动到下一个字符,获得字符直接*pi
    2. 不带ex的只资磁非负整数
    3. out使用了函数堆栈,在数比较小时快,int差不多,longlong级别建议改写成手工字符堆栈

    update

    附上蒟蒻现在已经敲熟了的gi,非常简单,可以直接应用在输入数据中只有整数的考试题目中,直接替代getchar()式的快读,并不容易写挂。

    #define gc if(++ip==ie)fread(ip=buf,1,SZ,stdin)
    const int SZ=1<<19;
    char buf[SZ],*ie=buf+SZ,*ip=ie-1;
    inline int gi(){
        gc;while(*ip<'-')gc;
        bool f=*ip=='-';if(f)gc;
        int x=*ip&15;gc;
        while(*ip>'-'){x*=10;x+=*ip&15;gc;}
        return f?-x:x;
    }
    
  • 相关阅读:
    [Makefile] 递归编译的Makefile的实现
    python中出现 IndentationError:unindent does not match any outer indentation level
    Python3 编译中文字串报错解决方案
    linux下创建和删除软、硬链接
    免费的编程中文书籍索引
    starUML建立时序图
    UML 学习地址
    使用 Addr2line 将函数地址解析为函数名
    WIFI基本知识整理
    Ubuntu16.04 安装Python3.6 报错
  • 原文地址:https://www.cnblogs.com/flashhu/p/8723447.html
Copyright © 2011-2022 走看看