avatar CyanTachyon's Blog

CyanTachyon的个人博客

  • 首页
  • 分类
  • 标签
  • 归档
  • 关于
首页 c++快读快写
文章

c++快读快写

发表于 2023/03/26 更新于 2025/04/12
作者 CyanTachyon
5 分钟阅读

c++快读快写

  1. cin换成in,cout换成out,cerr换为err
  2. 输出换行:out<<endln err<<endln
  3. 输出清空缓存(flush): out<<flush err<<flush (不手动清理则在程序结束时自动清理)
  4. get line:string s;in.getline(s);或char s[10000];getline(s);
  5. 支持int128,支持while(in>>x)和while(in.getline(s))
  6. 输出设置小数精度out<<precision(6)(默认:$6$)
  7. 若小数位不足是否补$0$out<<force(false)(默认:$false$)
  8. 小于精度的部分是否四舍五入(否则截断)out<<round(true)(默认:$true$)
  9. 整数长度不足时添加前导$0$out<<lead(1)(默认:$1$)
  1. 默认缓存区大小为in out 各1MB($2^{20}$字节) err缓存为$1$字节

  2. 修改缓存大小: 例如将out的缓存设置为$2^{10}$字节,将$179$行的out(stdout)改为out(stdout,1<<10)

  3. 程序异常退出($RE$)可能导致out的缓存没有清空(没被输出),将缓存区设为$1$字节可避免这一情况,但会导致效率下降.(若希望提高err的效率或避免out缓存没被清空,可修改它们的缓存大小)

  4. getchar(),putchar(c),puts(s)现在应该使用in.getc(),out.putc(c),out<<s否则可能导致输出错乱

  5. 文件输入输出可以使用freopen重定向stdio也可以创建新的对象,例:

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    struct Out out(stdout),err(stderr,1);
    struct In in(stdin);
       
    struct Out fout0("out0.out"); //创建文件输出,输出到"out0.out"缓存为2^20字节
    struct Out fout1("out1.out",1024);//创建文件输出,输出到"out1.out"缓存为1024字节
    struct In fin0("in0.in");//创建文件输入,从"in0.in"读入,缓存2^20字节
    struct In fin1("in1.in",1024);//创建文件输入,从"in1.in"读入,缓存1024字节
       
    string x;
    in>>x;//标准输入
    out<<x;//标准输出
    err<<x;//标准错误输出
       
    fin0>>x;//从"in0.in"输入
    fout0<<x;//输出到"out0.out"
    
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <bits/stdc++.h>
using namespace std;
struct Out
{
private:
    FILE*file;
    char*buf,*p1,*pend;
    int precision=6;
    bool force=false;
    bool round=true;
    int wight=0;
public:
    explicit Out(FILE*file,int size=1<<20):file(file),buf(new char[size]),p1(buf),pend(buf+size){}
    explicit Out(const string&file,int size=1<<20):Out(fopen(file.c_str(),"w"),size){}
    Out(const Out&)=delete;
    Out&operator=(const Out&)=delete;
    Out(Out&&)=delete;
    Out&operator=(Out&&)=delete;
    void setPrecision(int x){precision=x;}
    void setForce(bool x){force=x;}
    void setRound(bool x){round=x;}
    void setLead(int x){wight=x;}
    ~Out(){flush();delete[] buf;}
    void flush(){fwrite(buf,1,p1-buf,file),p1=buf;}
    void putc(char c){*p1++=c;if (p1==pend)flush();}
#define retp(x) template<class T> typename enable_if<x,Out>::type
#define lmt(x) x<T>::value
    retp(lmt(is_unsigned)&&lmt(is_integral))&operator<<(T x)
    {
        static char s[50];
        int len=0;
        do
        {
            s[len++]=x%10+'0';
            x/=10;
        } while (x||len<wight);
        while (len--)putc(s[len]);
        return *this;
    }
    retp(lmt(is_signed)&&lmt(is_integral))&operator<<(T x)
    {
        make_unsigned_t<T> y=x;
        if (x<0)putc('-'),y=-y;
        *this<<y;
        return *this;
    }
    retp(lmt(is_floating_point))&operator<<(T x)
    {
        if (x!=x)putc('N'),putc('a'),putc('N');
        else if (x==numeric_limits<T>::infinity())putc('I'),putc('n'),putc('f');
        else if (x==-numeric_limits<T>::infinity())putc('-'),putc('I'),putc('n'),putc('f');
        else
        {
            if (x<0)putc('-'),x=-x;
            if (round)x+=0.5*pow(0.1,precision);
            auto t=static_cast<intmax_t>(x);
            *this<<t;
            x-=t;
            x*=pow(10,precision);
            t=static_cast<intmax_t>(x);
            while (t%10==0&&t&&!force)t/=10;
            if (t||force)putc('.'),*this<<t;
        }
        return *this;
    }
#undef retp
#undef lmt
    Out&operator<<(const char*s){while (*s)putc(*s++);return *this;}
    Out&operator<<(const string&s){for (auto c:s)putc(c);return *this;}
    Out&operator<<(const char&c){putc(c);return *this;}
    Out&operator<<(const function<void(Out&)>&f){f(*this);return *this;}
};
const function<void(Out&)> endln=[](Out&o){o.putc('\n');};
const function<void(Out&)> flush=[](Out&o){o.flush();};
function<void(Out&)> precision(int n){return [n](Out&o){o.setPrecision(n);};}
function<void(Out&)> force(bool b){return [b](Out&o){o.setForce(b);};}
function<void(Out&)> round(bool b){return [b](Out&o){o.setRound(b);};}
function<void(Out&)> lead(int n){return [n](Out&o){o.setLead(n);};}
struct In
{
private:
    FILE*file;
    char*buf,*p1,*pend;
    bool eof;
    bool rd=true;
    void read(){p1=buf;pend=buf+fread(buf,1,pend-buf,file);eof=pend==p1;}
public:
    explicit In(FILE*file,int size=1<<20):file(file),buf(new char[size]),p1(buf+size),pend(buf+size),eof(false){}
    explicit In(const string&file,int size=1<<20):In(fopen(file.c_str(),"r"),size){}
    In(const In&)=delete;
    In&operator=(const In&)=delete;
    In(In&&)=delete;
    In&operator=(In&&)=delete;
    ~In(){delete[] buf;}
    char getc(){if (p1==pend)read();return eof?(char)-1:*p1++;}
    char peek(){if (p1==pend)read();return eof?(char)-1:*p1;}
    explicit operator bool ()const{return rd;}
#define retp(x) template<class T> typename enable_if<x,In>::type
#define lmt(x) x<T>::value
    retp(lmt(is_unsigned)&&lmt(is_integral))&operator>>(T&x)
    {
        rd=false;
        x=0;
        char c;
        while (!isdigit(c=getc())&&c!=-1);
        for (;isdigit(c);c=getc())x=x*10+c-'0',rd=true;
        return *this;
    }
    retp(lmt(is_signed)&&lmt(is_integral))&operator>>(T&x)
    {
        rd=false;
        x=0;
        char c;
        while (!isdigit(c=getc())&&c!='-'&&c!=-1);
        bool neg=false;
        if (c=='-')neg=true,c=getc();
        for (;isdigit(c);c=getc())x=x*10+(neg?'0'-c:c-'0'),rd=true;
        return *this;
    }
    retp(lmt(is_floating_point))&operator>>(T&x)
    {
        rd=false;
        x=0;
        char c;
        while (!isdigit(c=getc())&&c!='-'&&c!=-1);
        bool neg=false;
        if (c=='-')neg=true,c=getc();
        for (;isdigit(c);c=getc())x=x*10+(neg?'0'-c:c-'0'),rd=true;
        if (c=='.')
        {
            c=getc();
            T y=0;
            int cnt=0;
            for (;isdigit(c);c=getc())y=y*10+(neg?'0'-c:c-'0'),cnt++;
            x+=y*pow(0.1,cnt);
        }
        return *this;
    }
#undef retp
#undef lmt
    In&operator>>(char *s)
    {
        rd=false;
        while (isspace(*s=getc())&&*s!=-1);
        while (!isspace(*++s=getc())&&*s!=-1)rd=true;
        *s=0;
        return *this;
    }
    In&operator>>(string&s)
    {
        rd=false;
        s.clear();
        char c;
        while (isspace(c=getc())&&c!=-1);
        for (;!isspace(c)&&c!=-1;c=getc())s+=c,rd=true;
        return *this;
    }
    In&operator>>(char&c)
    {
        while (isspace(c=getc())&&c!=-1){}
        rd=c!=-1;
        return *this;
    }
    In&getline(char*s)
    {
        while ((*s=getc())=='\n'&&*s!=-1){}
        while ((*++s=getc())!='\n'&&*s!=-1)rd=true;
        return *s=0,*this;
    }
    In&getline(string&s)
    {
        s.clear();
        char c;
        while ((c=getc())=='\n'&&c!=-1){}
        for (;c!='\n'&&c!=-1;c=getc())s+=c,rd=true;
        return *this;
    }
};
struct Out out(stdout),err(stderr,1);
struct In in(stdin);
develop, cpp
cpp工具
本文由作者按照 CC BY-NC 4.0 进行授权, 未经许可不得转载。
分享

最近更新

  • SubQuiz Docs
  • Hash&KMP
  • c++快读快写
  • Java快速入门指南
  • Kotlin序列化多态

热门标签

develop kotlin cpp工具 java project vue 信息作业

外部链接

  •  此博客的 Github 仓库
  •  此博客的评论仓库

相关文章

2022/11/30

Hash&KMP

Hash 哈希表 OI-WIKI 哈希冲突 1. 开散列法 在遇到$hash$值相同的不同$key$值时,通过邻接表存储所有$key$值. 优点: 实现简单方便 缺点: 在使用时,需要根据情况动态开空间 2. 闭散列法 在遇到$hash$值相同的不同$key$值时,向后寻找未被使用的$hash$值,存入$key$值 优点: 在使用过程中,不需要再开更多空间...

2024/11/28

FuckBrainfuck

FuckBrainfuck 先叠甲 以下内容纯属娱乐,不具有任何实际意义。我无法保证我的实现没有问题,如有问题欢迎指出。 Brainfuck简述 若你已经知道何为Brainfuck,请跳过此部分. Brainfuck是一种极简单的编程语言,由Urban Müller于1993年创造。它只包含八个命令,分别是&gt; &lt; + - [ ] . ,。它的内存模型是一个无限长度的字节数...

2024/11/25

Kotlin序列化多态

Kotlin序列化多态 关于Kt多态简述 当在序列化中出现多态时,会在序列化结果中带上类判别器(classDiscriminator),以区分不同的class。默认为type,并且可以通过classDiscriminator修改。也可以通过classDiscriminatorMode修改何时添加判别器 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1...

Hash&KMP

Java快速入门指南

© 2025 CyanTachyon. 保留部分权利。

|

本站采用 Jekyll 主题 Chirpy

热门标签

develop kotlin cpp工具 java project vue 信息作业

发现新版本的内容。