C++输入挂

模板:
只适用于整数的输入

#include <iostream> 
using namespace std;
inline bool scan_d(int &num)  {
        char in;
		bool IsN=false;
        in=getchar();
        if(in==EOF) 
		return false;
        while(in!='-'&&(in<'0'||in>'9')) 
		in=getchar();
        if(in=='-')
		{ 
		    IsN=true;
			num=0;
		}
        else 
		num=in-'0';
        while(in=getchar(),in>='0'&&in<='9')
		{
            num*=10,num+=in-'0';
        }
        if(IsN) 
		num=-num;
        return true;
}
int main()
{
	int a;
	scan_d(a);
	return 0;
}

比scanf快四倍!

超高效输入:

namespace fastIO {
#define BUF_SIZE 100000
	bool IOerror = 0;
	inline char nc() {
		static char buf[BUF_SIZE], * p1 = buf + BUF_SIZE, * pend = buf + BUF_SIZE;
		if (p1 == pend) {
			p1 = buf;
			pend = buf + fread(buf, 1, BUF_SIZE, stdin);
			if (pend == p1) {
				IOerror = 1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
	}
	inline void read(int& x) {
		char ch;
		while (blank(ch = nc()));
		if (IOerror) return;
		for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
	}
#undef BUF_SIZE
};
using namespace fastIO;
发布了23 篇原创文章 · 获赞 31 · 访问量 1106

猜你喜欢

转载自blog.csdn.net/weixin_45333771/article/details/103554409