IO优化(玄学版,非正式)

chen_zhe 沙雕 2020-08-20 11:08:48 2020-08-20 17:32:07 0

用法:

IO::io cio;
int m, n, k;
cio >> m >> n >> k;
cio << m << sp << n << sp << k << el; //sp为空格,el为换行符

如果要读入负数,不能直接

cio >> 变量;

需要

cio.get(变量);

支持输入输出整数,字符串,字符,字符数组,不能输出小数

注意:

如果程序中用到了freopen,需要在freopen之后使用

IO::io cio

e.g

snip……

int main()
{
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
    IO::cio;
    int n;
    cio >> n;
}

代码:

#include <stdio.h> 
#include <string>

#define sp ' '
#define el '\n'

namespace IO
{
	char ibuf[100 << 20], * s = ibuf, f, obuf[1 << 20], * p = obuf;
	
	struct io
	{
		
		io (FILE *STDIN = stdin)
    	{
        	fread(s = ibuf, 1, 100 << 20, STDIN);
    	}
    	
    	inline io operator >> (register int &u)
    	{
        	u = 0;
        	while (* s < 48  || * s > 57) s ++;
        	while (* s >= 48 && * s <= 57)
           		u = u * 10 + * s ++ - 48;
           	return * this;
    	}
    	
    	inline void get (register int &u)
    	{
    		u = 0, f = 1;
        	while (* s < 48  || * s > 57)
        	if (* s ++ == '-')
			{
				f = -1;
				break;
			}
        	while (* s < 48  || * s > 57) * s ++;
        	while (* s >= 48 && * s <= 57)
           		u = u * 10 + * s ++ - 48;
           	u *= f;
		}
		
    	inline io operator >> (register char &ch)
    	{
    		ch = * s ++;
    		return * this;
		}
		
		inline io operator >> (register char * ch)
		{
			while (* s != ' ' && * s != '\n') * ch ++ = * s ++;
			return * this;
		}
		
		inline io operator >> (register std::string &str)
		{
			while (* s != ' ' && * s != '\n') str.append(1, * s ++);
			return * this;
		}
		
    	inline io operator << (register int u)
    	{
    		char obuf[105], * s = obuf;
    		if (u < 0) u = - u, * s ++ = '-';
			register int x = 1;
			while (x <= u) x *= 10;
			x /= 10;
			while (x) * s ++ = u / x + 48, u -= u / x * x, x /= 10;
			fwrite(obuf, 1, s - obuf, stdout);
			return * this;
		}
		
		inline io operator << (register char ch)
		{
			putchar(ch);
			return * this;
		}
		
		inline io operator << (register std::string str)
		{
			for (int i = 0; i < str.size(); i ++)
			* p ++ = str[i];
			fwrite(obuf, 1, p - obuf, stdout);
			return * this;
		}
	};
};
{{ vote && vote.total.up }}

共 4 条回复

chen_zhe 沙雕

WCH NB!吊打集训队!AK IOI!

pikahuan 逗比

张齐松NB!我就没这思路

chen_zhe 沙雕

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%FZH,FZH AK IOI,吊打集训队!!!!!

Duke

%%%