跳到正文

科技·工程

【模板】快速读入

myster1ous约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1
C++
class fastinputstream {
	private:
		static const int bufsize = 16384;
		std::streambuf *fb;
		char buf[bufsize], *s, *t;

	public: fastinputstream() {
		fb = std::cin.rdbuf();
		s = t = buf;
	}

	public: char getc() {
		if (s == t) {
			s = buf;
			t = buf + fb->sgetn(buf, bufsize);
		}
		char get = *s;
		return s++, get;
	}

	public: template<class _int>
	void read(_int& x) {
		char ch = getc();
		int sign = 1; x = 0;
		while (!isdigit(ch)) {
			if (ch == '-') sign = -sign;
			ch = getc();
		}
		while (isdigit(ch)) {
			x = x * 10 + ch - '0';
			ch = getc();
		}
		x *= sign;
	}

	public: template<class _int, class ..._args>
	void read(_int& a, _args& ... args) {
		read(a);
		read(args ... );
	}
} input;

class fastoutputstream {
	private:
		static const int bufsize = 16384;
		std::streambuf *fb;
		char buf[bufsize], *s, *t;

	public: fastoutputstream() {
		fb = std::cout.rdbuf();
		s = buf, t = buf + bufsize;
	}

	public: void write(char ch) {
		fb->sputc(ch);
	}

	public: void write(std::string s) {
		for (auto i : s) fb->sputc(i);
	}

	public: void write(const char *ch) {
		int ln = strlen(ch);
		for (int i = 0; i < ln; i++)
			fb->sputc(ch[i]);
	}

	public: template<class _int>
	void write(_int x) {
		int len = 0;
		static char stack[64];
		if (x < 0) {
			fb->sputc('-');
			x = -x;
		}
		if (!x) {
			fb->sputc('0');
			return;
		}
		while (x) {
			stack[len++] = x % 10 + '0';
			x /= 10;
		}
		for (int i = len - 1; i >= 0; i--)
			fb->sputc(stack[i]);
	}

	public: template<class _int, class ..._args>
	void write(_int a, _args ... args) {
		write(a);
		write(args ... );
	}
} output;
原文