v1.0

用法

通过 read()print() 进行输入输出, 也可以用 cin , cout (我已经define了)。

千万不要快读快写和 cstdio 混用!!!!

read() 可以传入任意c++关键字类型和string(小数也可以)。

read()print() 也可以传入多个参数,即使不同类型也可以。(c++98不支持)

int a; char b;
read(a, b); // OK, no problem

输出小数时,使用setpcs(x)来保留精度(四舍五入)。

double x;
print(setpcs(5), x);
cout << setpcs(5) << x;

读入 char 类型时,会自动过滤掉空格(读入第一个不是空格的字符),效果跟cin一样。

如果想要读入一整行,可以用getline(cin, string)(字符数组的我懒得写了)。

根据使用习惯也可以用 a = read<int>() 的方式读入。

read() 函数会返回成功读入了几个数据,可以用来判断是否到达读入文件结尾。
cin >> a 也会返回是否读入成功。

while(read(a,b) == 2);
while(cin >> a >> b);

code

namespace Octane {
// non terrae plus ultra
#define OCTANE
#define BUFFER_SIZE 100000
#define ll long long
#define db double
#define ldb long double
char ibuf[BUFFER_SIZE], obuf[BUFFER_SIZE];
char *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#ifdef ONLINE_JUDGE
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,BUFFER_SIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+BUFFER_SIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#endif // fread in OJ, getchar in local

#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<=32&&ch!=EOF)
#define isseen(ch) (ch>32)

const ll pow10[] = {
(ll)1e0, (ll)1e1, (ll)1e2, (ll)1e3, (ll)1e4, (ll)1e5,
(ll)1e6, (ll)1e7, (ll)1e8, (ll)1e9, (ll)1e10, (ll)1e11,
(ll)1e12, (ll)1e13, (ll)1e14, (ll)1e15, (ll)1e16, (ll)1e17, (ll)1e18,
};

struct Octane_t {
~Octane_t() {
fwrite(obuf, p3-obuf, 1, stdout);
}
bool flag = false;
operator bool() {
return flag;
}
}io;

template<typename T> inline T read() {
T s = 0; int w = 1; char ch;
while(ch=getchar(), !isdigit(ch)&&(ch!=EOF))
if(ch == '-') w = -1;
if(ch == EOF) return 0;
while(isdigit(ch))
s = s*10+ch-48, ch=getchar();
if(ch == '.') {
ll flt = 0; int cnt = 0;
while(ch=getchar(), isdigit(ch))
if(cnt < 18) flt=flt*10+ch-48, cnt++;
s += (db)flt/pow10[cnt];
}
return s *= w;
}
template<typename T> inline bool read(T &s) {
s = 0; int w = 1; char ch;
while(ch=getchar(), !isdigit(ch)&&(ch!=EOF))
if(ch == '-') w = -1;
if(ch == EOF) return false;
while(isdigit(ch))
s = s*10+ch-48, ch=getchar();
if(ch == '.') {
ll flt = 0; int cnt = 0;
while(ch=getchar(), isdigit(ch))
if(cnt < 18) flt=flt*10+ch-48, cnt++;
s += (db)flt/pow10[cnt];
}
return s *= w, true;
}
inline bool read(char &s) {
while(s = getchar(), isspace(s));
return s != EOF;
}
inline bool read(char *s) {
char ch;
while(ch=getchar(), isspace(ch));
if(ch == EOF) return false;
while(isseen(ch)) *s++ = ch, ch=getchar();
*s = '\000'; return true;
}
template<typename T> void print(T x) {
static int t[20]; int top = 0;
if(x < 0) putchar('-'), x = -x;
do { t[++top] = x%10; x /= 10; } while(x);
while(top) putchar(t[top--]+48);
}
struct empty_type{}; int pcs = 8;
empty_type setpcs(int cnt) {
return pcs = cnt, empty_type();
}
inline void print(empty_type x){}
inline void print(double x) {
if(x < 0) putchar('-'), x = -x;
x += 5.0 / pow10[pcs+1];
print((ll)(x)); x -= (ll)(x);
if(pcs != 0) putchar('.');
for(int i = 1; i <= pcs; i++)
x *= 10, putchar((int)x+'0'), x -= (int)x;
}
inline void print(float x) {
if(x < 0) putchar('-'), x = -x;
x += 5.0 / pow10[pcs+1];
print((ll)(x)); x -= (ll)(x);
if(pcs != 0) putchar('.');
for(int i = 1; i <= pcs; i++)
x *= 10, putchar((int)x+'0'), x -= (int)x;
}
inline void print(char x) {
putchar(x);
}
inline void print(char *x) {
for(int i = 0; x[i]; i++)
putchar(x[i]);
}
inline void print(const char *x) {
for(int i = 0; x[i]; i++)
putchar(x[i]);
}

// support for string
#ifdef _GLIBCXX_STRING
inline bool read(std::string& s) {
s = ""; char ch;
while(ch=getchar(), isspace(ch));
if(ch == EOF) return false;
while(!isspace(ch))
s += ch, ch = getchar();
return true;
}
inline void print(std::string x) {
for(string::iterator i = x.begin(); i != x.end(); i++)
putchar(*i);
}
inline bool getline(Octane_t &io, string s) {
s = ""; char ch = getchar();
if(ch == EOF) return false;
while(ch != '\n' and ch != EOF)
s += ch, ch = getchar();
return true;
}
#endif

// support for initializer_list
#if __cplusplus >= 201103L
template<typename T, typename... T1>
inline int read(T& a, T1& ...other) {
return read(a)+read(other...);
}
template<typename T, typename... T1>
inline void print(T a, T1... other) {
print(a); print(other...);
}
#endif

// give up iostream
template<typename T>
Octane_t& operator >> (Octane_t &io, T &b) {
return io.flag=read(b), io;
}
Octane_t& operator >> (Octane_t &io, char *b) {
return io.flag=read(b), io;
}
template<typename T>
Octane_t& operator << (Octane_t &io, T b) {
return print(b), io;
}

#define cout io
#define cin io
#define endl '\n'
#undef ll
#undef db
#undef ldb
}
using namespace Octane;

纯享版代码

namespace Octane {
// non terrae plus ultra
#define OCTANE
#define BUFFER_SIZE 100000
#define ll long long
#define db double
#define ldb long double
char ibuf[BUFFER_SIZE], obuf[BUFFER_SIZE];
char *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#ifdef ONLINE_JUDGE
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,BUFFER_SIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+BUFFER_SIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#endif // fread in OJ, getchar in local
#define isdigit(ch) (ch>47&&ch<58) //////////////////////////////
#define isspace(ch) (ch<=32&&ch!=EOF) ///////////////////////////
#define isseen(ch) (ch>32) //////////////////////////////////////
const ll pow10[]={(ll)1e0,(ll)1e1,(ll)1e2,(ll)1e3,(ll)1e4,(ll)1e5
,(ll)1e6,(ll)1e7, (ll)1e8, (ll)1e9, (ll)1e10, (ll)1e11, (ll)1e12
,(ll)1e13,(ll)1e14,(ll)1e15,(ll)1e16, (ll)1e17, (ll)1e18,};;;;;;;
struct Octane_t { ~Octane_t() { fwrite(obuf, p3-obuf, 1, stdout);
}bool flag = false;operator bool() { return flag; } }io; template
<typename T> inline T read() { T s = 0; int w = 1; char ch; while
(ch=getchar(), !isdigit(ch)&&(ch!=EOF)) if(ch == '-') w = -1; if(
ch == EOF) return 0; while(isdigit(ch)) s = s*10+ch-48,ch=getchar
();if(ch=='.'){ll flt=0;int cnt=0;while(ch=getchar(),isdigit(ch))
if(cnt < 18) flt= flt*10+ch-48, cnt++; s += (db)flt/pow10[cnt]; }
return s *= w; } template <typename T> inline bool read(T &s) { s
= 0; int w=1;char ch; while(ch=getchar(),!isdigit(ch)&&(ch!=EOF))
if(ch=='-') w= -1; if(ch == EOF) return false; while(isdigit(ch))
s = s*10+ch-48, ch=getchar(); if(ch == '.') { ll flt = 0; int cnt
= 0; while(ch=getchar(),isdigit(ch))if(cnt< 18) flt=flt*10+ch-48,
cnt++;s+=(db)flt/pow10[cnt]; } return s *= w, true; } inline bool
read(char &s){s=getchar();while(isspace(s)) s = getchar(); return
s != EOF; } inline bool read(char *s){char ch;while(ch=getchar(),
isspace(ch));if(ch==EOF)return false;while(isseen(ch)) *s++ = ch,
ch=getchar();*s ='\000'; return true; } template<typename T> void
print(T x){static int t[20]; int top = 0; if(x < 0) putchar('-'),
x=-x; do{t[++top] = x%10; x /= 10; } while(x); while(top) putchar
(t[top--]+48);}struct empty_type{};int pcs = 8; empty_type setpcs
(int cnt) { return pcs = cnt, empty_type(); } inline void print
(empty_type x){ } inline void print(double x) { if(x < 0) putchar
('-'), x = -x; x += 5.0 / pow10[pcs+1]; print((ll)(x)); x -= (ll)
(x);if(pcs!=0)putchar('.');for(int i = 1; i <= pcs; i++) x *= 10,
putchar((int)x+'0'),x-= (int)x; } inline void print(float x) { if
(x < 0) putchar('-'), x = -x; x += 5.0 / pow10[pcs+1]; print((ll)
(x));x-=(ll)(x);if(pcs!= 0)putchar('.'); for(int i = 1; i <= pcs;
i++)x*= 10, putchar((int)x+'0'), x -= (int)x; } inline void print
(char x){putchar(x);}inline void print(char *x){for(int i=0;x[i];
i++)putchar(x[i]); } inline void print(const char *x) { for(int i
= 0; x[i]; i++) putchar(x[i]);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;}
// support for string ///////////////////////////////////////////
#ifdef _GLIBCXX_STRING //////////////////////////////////////////
inline bool read(std::string&s){s="";char ch; while(ch=getchar(),
isspace(ch)); if(ch == EOF)return false;while(!isspace(ch))s+=ch,
ch = getchar(); return true; } inline void print(std::string x) {
for(string::iterator i=x.begin(); i != x.end(); i++) putchar(*i);
}inline bool getline(Octane_t&io,string s){s="";char ch = getchar
();if(ch==EOF)return false;while(ch!='\n' and ch != EOF) s += ch,
ch = getchar(); return true;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; }
#endif //////////////////////////////////////////////////////////
// support for initializer_list /////////////////////////////////
#if __cplusplus >= 201103L /////////////////////////////////////
template<typename T,typename...T1>inline int read(T&a,T1&...other
) { return read(a)+read(other...);} template<typename T, typename
... T1> inline void print(T a, T1... other){print(a); print(other
...);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; }
#endif //////////////////////////////////////////////////////////
// give up iostream ////////////////////////////////////////////
template<typename T> Octane_t& operator >> (Octane_t &io, T &b) {
return io.flag=read(b), io; } Octane_t& operator >> (Octane_t &io
,char*b){return io.flag=read(b),io;}template<typename T> Octane_t
& operator << (Octane_t &io, T b) { return print(b), io;;;;;;;; }
#define cout io /////////////////////////////////////////////////
#define cin io //////////////////////////////////////////////////
#define endl '\n' ///////////////////////////////////////////////
#undef ll ///////////////////////////////////////////////////////
#undef db ///////////////////////////////////////////////////////
#undef ldb //////////////////////////////////////////////////////
}
using namespace Octane;

v1.5

用法

读入用法和原来一样,但是输出不一样。

输出用的是 print(format string, values)

如果要输出一个变量,就用 print("{}", a)

一行输出两个变量并换行可以用 print("{} {}\n", a, b)

或者如果有多测题可以用 print("Cases #{}:\n", i)

用法类似于 printf,但是不需要指定变量的类型,可以自动推导类型,一个 {} 代表一个变量,如果真的要输出 {} 这两个字符,可以用 print("ˋ{ˋ}"), 也就是把 ˋ 当作了 {} 的转义字符,如果想要输出 ˋ 请使用 print("ˋˋ"),当然除了这俩以外其他的转义字符还是原生的 \

这个快写还很不成熟,不支持小数输出,而且 {} 里必须什么也没有,空格也不行。

如果格式串不符合要求输出会变得有点奇怪,这不是我的问题,It’s not a bug, it’s a feature.

暂不支持小数读入输出

代码

namespace Octane {
// non terrae plus ultra
#define OCTANE
#define BUFFER_SIZE 100000
#define ll long long
#define db double
#define ldb long double
char ibuf[BUFFER_SIZE], obuf[BUFFER_SIZE];
char *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#ifdef ONLINE_JUDGE
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,BUFFER_SIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+BUFFER_SIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#endif // fread in OJ, getchar in local

#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<=32&&ch!=EOF)
#define isseen(ch) (ch>32)

struct Octane_t {
~Octane_t() {
fwrite(obuf, p3-obuf, 1, stdout);
}
bool flag = false;
operator bool() {
return flag;
}
}io;

template<typename T> inline T read() {
T s = 0; int w = 1; char ch;
while(ch=getchar(), !isdigit(ch)&&(ch!=EOF))
if(ch == '-') w = -1;
if(ch == EOF) return 0;
while(isdigit(ch))
s = s*10+ch-48, ch=getchar();
return s *= w;
}
template<typename T> inline bool read(T &s) {
s = 0; int w = 1; char ch;
while(ch=getchar(), !isdigit(ch)&&(ch!=EOF))
if(ch == '-') w = -1;
if(ch == EOF) return false;
while(isdigit(ch))
s = s*10+ch-48, ch=getchar();
return s *= w, true;
}
inline bool read(char &s) {
while(s = getchar(), isspace(s));
return s != EOF;
}
inline bool read(char *s) {
char ch;
while(ch=getchar(), isspace(ch));
if(ch == EOF) return false;
while(isseen(ch)) *s++ = ch, ch=getchar();
*s = '\000'; return true;
}


template<typename T>
void printv(T a)
{
if(a < 0) putchar('-'), a = -a;
if(a >= 10) printv(a/10);
putchar(a%10+'0');
}
inline void printv(char c)
{
putchar(c);
}
inline void printv(char *s)
{
for(; *s; s++)
putchar(*s);
}
inline void printv(const char *s)
{
for(int i = 0; s[i]; i++)
putchar(s[i]);
}
inline void printv(bool a)
{
if(a) putchar('1');
else putchar('0');
}


// support for string
#ifdef _GLIBCXX_STRING
inline bool read(std::string& s) {
s = ""; char ch;
while(ch=getchar(), isspace(ch));
if(ch == EOF) return false;
while(!isspace(ch))
s += ch, ch = getchar();
return true;
}
inline bool getline(Octane_t &io, std::string s) {
s = ""; char ch = getchar();
if(ch == EOF) return false;
while(ch != '\n' and ch != EOF)
s += ch, ch = getchar();
return true;
}
inline void printv(const std::string &a)
{
for(auto i = a.begin(); i != a.end(); ++i)
putchar(*i);
}
#endif

void print(const char *p)
{
printv(p);
}
template<typename T>
inline void print(const char *p, T first)
{
int n = strlen(p)-1;
for(int i = 0; i <= n; i++)
{
if(p[i] == '`')
{
putchar(p[++i]);
continue;
}
else if(p[i] == '{')
{
printv(first);
i++; continue;
}
else
putchar(p[i]);
}
}

// support for many values
#if __cplusplus >= 201103L
template<typename T, typename... T1>
inline int read(T& a, T1& ...other) {
return read(a)+read(other...);
}
template<typename T1, typename... T2>
void print(const char *p, T1 first, T2 ...other)
{
int n = strlen(p)-1;
for(int i = 0; i <= n; i++)
{
if(p[i] == '`')
{
putchar(p[++i]);
continue;
}
else if(p[i] == '{')
{
printv(first);
print(p+i+2, other...);
return void();
}
else
putchar(p[i]);
}
}

#endif

// give up iostream
template<typename T>
Octane_t& operator >> (Octane_t &io, T &b) {
return io.flag=read(b), io;
}
Octane_t& operator >> (Octane_t &io, char *b) {
return io.flag=read(b), io;
}
template<typename T>
Octane_t& operator << (Octane_t &io, T b) {
return printv(b), io;
}

#define cout io
#define cin io
#define endl '\n'
#undef ll
#undef db
#undef ldb
} using namespace Octane;

纯享版代码

namespace Octane {
//non terrae plus ultra dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#define OCTANE // dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#define BUFFER_SIZE 100000 // dqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#define ll long long // dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#define db double // dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#define ldb long double // dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
char ibuf[100000], obuf[100000], *p1=ibuf,*p2=ibuf,*p3=obuf;
#ifdef ONLINE_JUDGE//dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#define getchar() ((p1==p2) and (p2=(p1=ibuf)+fread(ibuf,1,\
BUFFER_SIZE,stdin),p1==p2)?(EOF):(*p1++)) // dqrdqrdqrdqrdqr
#define putchar(x) ((p3==obuf+BUFFER_SIZE) && (fwrite(obuf,\
p3-obuf,1,stdout),p3=obuf),*p3++=x) // dqrdqrdqrdqrdqrdqrdqr
#endif// fread in OJ, getchar in local dqrdqrdqrdqrdqrdqrdqr
#define isdigit(ch) (ch>47&&ch<58)//dqrdqrdqrdqrdqrdqrdqrdqr
#define isspace(ch) (ch<=32&&ch!=EOF)//dqrdqrdqrdqrdqrdqrdqr
#define isseen(ch) (ch>32) // dqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
struct Octane_t{~Octane_t(){fwrite(obuf,p3-obuf, 1, stdout);
}bool flag=false;operator bool(){return flag;} }io; template
<typename T>inline T read(){T s=0; int w = 1; char ch; while
(ch=getchar(), !isdigit(ch)&&(ch!=EOF))if(ch == '-') w = -1;
if(ch == EOF) return 0; while(isdigit(ch)) s = s*10+ch-48,ch
=getchar(); return s *= w; } template<typename T>inline bool
read(T &s) { s = 0; int w = 1; char ch; while(ch = getchar()
,!isdigit(ch)&&(ch!=EOF))if(ch == '-') w = -1; if(ch == EOF)
return false;while(isdigit(ch))s = s*10+ch-48, ch=getchar();
return s*=w,true;}inline bool read(char &s){while(s= getchar
(), isspace(s)); return s != EOF; } inline bool read(char *s
){char ch=getchar();while(isspace(ch))ch= getchar();if(ch ==
EOF)return false;while(isseen(ch)) *s++ = ch, ch= getchar();
*s='\000';return true;}template<typename T> void printv(T a)
{if(a== 0){ putchar('0'); return void(); }static char st[65]
; int top = 0; if (a < 0) putchar ('-'), a = - a; while(a)st
[++top]='0'+a%10,a/=10;while(top)putchar(st[top--]);} inline
void printv(char c){putchar(c);}inline void printv(char *s){
for(int i=0;s[i];i++)putchar(s[i]);}inline void printv(const
char *s){ for(int i=0;s[i];i++) putchar(s[i]); } inline void
printv(bool a){ if(a != 0)putchar('1'); else putchar('0'); }
#ifdef _GLIBCXX_STRING // support for string dqrdqrdqrdqrdqr
inline bool read(std::string& s) { s = ""; char ch; while(ch
=getchar(), isspace(ch)); if(ch == EOF) return false; while(
!isspace(ch)) s+=ch,ch=getchar(); return true; } inline bool
getline(Octane_t &io,std::string s){s="";char ch= getchar();
if(ch==EOF)return false;while(ch!='\n' and ch !=EOF)s+=ch,ch
=getchar();return true;}inline void printv(const std::string
&a){for(auto i = a.begin(); i != a.end(); ++i) putchar(*i);}
#endif// dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
template<typename T>inline void print(const char *p,T first)
{ int n = strlen(p) - 1; for(int i = 0; i <= n; i++) { if(p[
i] == '`') { putchar(p[++ i]); continue; } else if ( p[i] ==
'{'){printv(first); i++; continue; } else putchar(p[i]); } }
#if __cplusplus >= 201103L // support for many values dqrdqr
template<typename T,typename... T1>inline int read(T& a, T1&
...other){return read(a)+read(other...); } inline void print
(const char *p) { printv(p); }template<typename T1, typename
... T2>void print(const char*p, T1 first, T2 ...other) { int
n=strlen(p)-1; for(int i = 0; i <= n; i++) { if(p[i] == '`')
{putchar(p[++i]);continue;}else if(p[i]=='{'){printv(first);
print(p+i+2,other...);return void();}else putchar(p[i]); } }
#endif // dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdq
template <typename T> Octane_t& operator >> (Octane_t &io, T
&b){return io.flag=read(b),io;}Octane_t& operator>>(Octane_t
&io, char *b){return io.flag=read(b), io;} template<typename
T>Octane_t&operator<<(Octane_t&io,T b){return printv(b),io;}
#define cout io// dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#define cin io // dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#define endl '\n' // dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#undef ll// dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#undef db// dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
#undef ldb//dqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqrdqr
} using namespace Octane;