跳到正文

算法·理论

自用 C++ 模板

_hud约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年5月7日
收录
2025年12月12日
更新
2026年10月30日
篇幅
约 7 分钟
获赞
1

版本

  1. v1当前

以下内容要求全文背诵

C++
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <cctype>
class GC {
private:
    char* addr_ = nullptr; char* cur_ = nullptr; char* end_ = nullptr;
    size_t size_ = 0;
    bool is_mapped_ = false;
    inline int get() {
        if(cur_ < end_) return static_cast<unsigned char>(*cur_++);
        return EOF;
    }
public:
    GC() {
        struct stat st;
        if(fstat(0, &st) == -1) { is_mapped_ = false; return; }
        size_ = st.st_size;
        if(size_ == 0) { is_mapped_ = false; return; }
        addr_ = static_cast<char*>(mmap(nullptr, size_, PROT_READ, MAP_PRIVATE, 0, 0));
        if(addr_ == MAP_FAILED) { is_mapped_ = false; return; }
        is_mapped_ = true, cur_ = addr_, end_ = addr_ + size_;
    }
    ~GC() { if(is_mapped_) munmap(addr_, size_); }
    int operator()() { return get(); }
} gc;
struct IO {
    #define isd(x) (c > 47 && c < 58)
	#define SIZE (1 << 20)
	char pbuf[SIZE], *pp;
    IO() : pp(pbuf) {}
	~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }
    template <class T>
	inline T read(T &x) {
		x = 0; char c; register int sign = 1;
		for(c = gc(); !isd(c); c = gc()) if(c == '-') sign = -1;
		for(; isd(c); c = gc()) x = (x << 1) + (x << 3) + (c & 15);
		return x *= sign;
	}
    template <typename T, typename... A>
    inline void read(T &x, A &...args) { read(x); read(args...); }
	inline void push(const char &c) {
		if(pp - pbuf == SIZE) fwrite(pbuf, 1, SIZE, stdout), pp = pbuf;
		*pp++ = c;
	}
    template<class T>
	inline void write(T x) {
		if(x < 0) x = -x, push('-');
		static int sta[40]; register int top = 0;
		do { sta[top++] = x % 10, x /= 10; } while(x);
		while(top) push(sta[--top] + '0');
	}
} io;
#define read io.read
#define write io.write

完整版

C++
#include <bits/stdc++.h>
using namespace std;
#define DEBUG // 调试开关
struct IO {
	#define SIZE (1 << 20)
	#define isdigit(x) (x > 47 && x < 58)
	char buf[SIZE], pbuf[SIZE], *p1, *p2, *pp;
	#ifdef DEBUG
	#else
	IO() : p1(buf), p2(buf), pp(pbuf) {}
	~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }
	#endif
	inline int gc() {
		#ifdef DEBUG
			return getchar();
		#endif
		if(p1 == p2) p2 = (p1 = buf) + fread(buf, 1, SIZE, stdin);
		return p1 == p2 ? ' ' : *p1++;
	}
	inline bool blank(char c) {
		return c == '\n' || c == '\r' || c == ' ' || c == '\t';
	}
	template <class T>
	inline T read(T &x) {
		double tmp = 1; int sign = 1; x = 0; char c;
		for(c = gc(); !isdigit(c); c = gc()) sign = (c == '-' ? -sign : sign);
		for(; isdigit(c); c = gc()) x = x * 10 + (c & 15);
		if(c == '.') for(; isdigit(c); c = gc())
			tmp /= 10, x += tmp * (c & 15);
		x *= sign;
		return x;
	}
    template <typename T, typename... Args>
    inline void read(T &x, Args &...args) { read(x); read(args...); }
	inline void read(char &c) { for(c = gc(); blank(c); c = gc()); }
	inline void read(char *s) {
		char c;
		for(c = gc(); blank(c); c = gc());
		for(; !blank(c); c = gc()) *s++ = c;
		*s = 0;
	}
	inline void push(const char &c) {
		#ifdef DEBUG
			putchar(c);
		#else
			if(pp - pbuf == SIZE) fwrite(pbuf, 1, SIZE, stdout), pp = pbuf;
			*pp++ = c;
		#endif
	}
	template <class T>
	inline void write(T x) {
		if(x < 0) x = -x, push('-');
		static int sta[40];
		T top = 0;
		do {
			sta[top++] = x % 10, x /= 10;
		} while(x);
		while(top) push(sta[--top] + '0');
	}
	template <class T>
	inline void write(T x, char lc) {
		write(x), push(lc);
	}
} io;
signed main() {

	return 0;
}
原文
存档评论1 条抓取于