跳到正文

个人记录

U534097 【模板】快读快写 C++ 题解代码

___Mr_Black___约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年2月12日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 5 分钟

版本

  1. v1当前
C++
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <cstring>
#include <string>
#include <vector>

namespace OIfast {
	
	/* ———————— 准备部分 ———————— */
	
	using namespace std ;
	
	typedef long long lint ;
	typedef long double dint ;
	
	// 开启/关闭 同步流
	#define BUFFER_SIZE lint(1 << 16)
	#define OIopen std::ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr) ;
	#define OIoff  std::ios::sync_with_stdio(true), std::cin.tie(&std::cout), std::cout.tie(&std::cin) ;
	
	/* ———————— 快读部分 ———————— */
	
	// 快读(字符)
	lint idx = false ;
	char buf[BUFFER_SIZE] ;
	inline char get_char() {
		if (idx >= BUFFER_SIZE) { // 缓存区耗尽
			idx = 0 ;
			lint b = read(STDIN_FILENO, buf, BUFFER_SIZE) ;
			if (b <= 0) return EOF ; // 读取失败或没有数据
		}
		return buf[idx ++] ; // 返回字符、更新指针
	}
	
	// 快读(数字)
	inline void get_lint(lint &x) {
		char c ;
		bool b = false ;
		for (c = get_char() ; !isdigit(c) ; c = get_char())
			if (c == '-') b = true ;
		for (x = 0 ; isdigit(c) ; c = get_char())
			x = (x << 3) + (x << 1) + (c ^ '0') ;
		if (b == true) x *= -1 ;
	}
	
	// 快读(字符串)
	inline void get_string(string &str, const char End) {
		char c = get_char() ;
		while (c == End) c = get_char() ;
		while (c != End && c != EOF) {
			str.push_back(c) ;
			c = get_char() ;
		}
		return ;
	}
	
	/* ———————— 快写部分 ———————— */
	
	// 快写(字符)
	lint Idx = false ;
	char Buf[BUFFER_SIZE] ;
	inline void put_char(const char &c) {
		if (Idx == BUFFER_SIZE) {
			write (STDOUT_FILENO, Buf, BUFFER_SIZE) ;
			Idx = 0 ;
		}
		Buf[Idx ++] = c ;
	}
	
	// 快写(数字)
	inline void put_lint(lint x, const char &End) {
		string str ;
		if (x == 0) {put_char('0') ; return ;}
		if (x <  0) {x *= -1, put_char('-') ;}
		while (x) str.push_back((x % 10) + '0'), x /= 10 ;
		for (lint i = str.size() - 1 ; i >= 0 ; i--)
			put_char(str[i]) ;
		put_char(End) ;
		return ;
	}
	
	// 快写(字符串)
	inline void put_string(const string &str, const char &End) {
		for (char c : str)
			put_char(c) ;
		put_char(End) ;
		return ;
	}
	
	// 刷新缓存区:确保所有数据都被写出
	inline void flush() {
		if (Idx > 0) {
			write (STDOUT_FILENO, Buf, Idx) ;
			Idx = 0 ;
		}
		return ;
	}
}

using namespace std ;
using namespace OIfast ;

lint test_int ;
string test_str ;

signed main () {
	
	lint num ;
	get_lint(num) ;
	put_lint(num, '\n') ;
	
	string str ;
	get_string(str, '\n') ;
	put_string(str, '\n') ;
	flush() ;
	
	return 0 ;
}
原文