专栏文章

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

个人记录参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@miq9xewc
此快照首次捕获于
2025/12/04 01:21
3 个月前
此快照最后确认于
2025/12/04 01:21
3 个月前
查看原文
CPP
#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 ;
}

评论

0 条评论,欢迎与作者交流。

正在加载评论...