C++快读快写详解

完整的读写模板(新式)

#define USEFASTERREAD 1

#define rg register
#define inl inline
#define DEBUG printf("qwq\n")
#define DEBUGd(x) printf("var %s is %lld", #x, ll(x))
#define DEBUGf(x) printf("var %s is %llf", #x, double(x))
#define putln putchar('\n')
#define putsp putchar(' ')
#define Rep(a, s, t) for(rg int a = s; a <= t; a++)
#define Repdown(a, t, s) for(rg int a = t; a >= s; a--)
typedef long long ll;
typedef unsigned long long ull;
#include<cstdio>

#if USEFASTERREAD
char In[1 << 20], *ss = In, *tt = In;
#define getchar() (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt) ? EOF : *ss++)
#endif
namespace IO {
	inl void RS() {freopen("test.in", "r", stdin), freopen("test.out", "w", stdout);}
	inl ll read() {
		ll x = 0, f = 1; char ch = getchar();
		for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
		for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0');
		return x * f;
	}
	inl void write(ll x) {
		if(x < 0) {putchar('-'); x = -x;}
		if(x >= 10) write(x / 10);
		putchar(x % 10 + '0');
	}
	inl void writeln(ll x) {write(x), putln;}
	inl void writesp(ll x) {write(x), putsp;}
}
using namespace IO;
template<typename T> inline T Max(const T& x, const T& y) {return y < x ? x : y;}
template<typename T> inline T Min(const T& x, const T& y) {return y < x ? y : x;}
template<typename T> inline void Swap(T& x, T& y) {T tmp = x; x = y; y = tmp;}
template<typename T> inline T Abs(const T& x) {return x < 0 ? -x : x;}

基本快读快写(旧式)

struct IO
{
	IO(int set = 0) {if(set) rs;} 
	void RS() {rs;} 
	template<typename T> inline IO r(T& x)const
	{
	    x = 0; T f = 1; char ch = getchar();
	    for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
	    for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0');
	    x *= f; return *this;
	}
	template<typename T> inline IO w(T x)const
	{
	    if(x < 0) {putchar('-'); x = -x;}
	    if(x >= 10) w(x / 10);
	    putchar(x % 10 + '0'); return *this;
	}
	template<typename T> inline IO wl(const T& x)const {w(x), putline; return *this;}
	template<typename T> inline IO ws(const T& x)const {w(x), putsp; return *this;}
	inline IO l() {putline; return *this;}
	inline IO s() {putline; return *this;}
}io;

更快的快读

代码

只要在基本的快读前加上

#define getchar() (tt == ss && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt) ? EOF : *ss++)
char In[1 << 20], *ss=In, *tt=In;

解释

In[]:用来缓存输入的东西的一个缓冲区
ss:指向当前读到的元素
tt:指向缓冲区的末尾

重定义的getchar()函数详解:
1)若ss!=tt,代表缓冲区还没读完,直接返回*ss,然后再ss++即可
2)若ss==tt,代表缓冲区已经读完
此时将ss重新赋值为In,然后tt赋值为ss+读入了的元素个数
a.若此时还是ss==tt,说明读入的字符个数为0,读不出东西了,到了文件末尾,返回EOF(文件末尾标识符)
b.否则返回*ss,然后再ss++即可

此快读使用注意:
1)这个快读的效率不知道比系统自带的getchar()高多少2333
2)根据题目所给的空间注意char In[]的大小,不要MLE了!
3)此快读一般用于文件读写。若要使用控制台,则应该在输入所有数据后使用Ctrl+Z输入EOF
4)考场上如果写不对不如不写,反正没有哪个出题人丧心病狂到卡起了系统自带的getchar()
5)使用了这个快读,就不能用系统自带的getchar(),scanf(),cin之类的输入方法了(因为它是搬运文件内容,一次搬一堆)

### Java 文件代码模板 #### 使用 `BufferedReader` 和 `BufferedWriter` 为了实现高效的文件操作,在Java中推荐使用带缓冲功能的输入输出流。这不仅提高了性能,还简化了编码过程。 对于文本文件的处理,可以选择基于字符的缓冲流——`BufferedReader`用于高效地取字符串;而`BufferedWriter`则适合用来出数据到文件[^4]。 下面是一个完整的例子来展示如何利用这些类完成文件的: ```java import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FastReadWriteExample { public static void main(String[] args) { String inputFilePath = "input.txt"; String outputFilePath = "output.txt"; try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath)); BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) { String currentLine; while ((currentLine = reader.readLine()) != null) { // Process the line as needed here. writer.write(currentLine); writer.newLine(); // Add a newline character after each write operation. } } catch (IOException e) { System.err.println("An error occurred during file processing."); e.printStackTrace(); } } } ``` 此程序首先定义两个路径变量分别指向源文件和目标文件的位置。通过尝试资源声明的方式打开`BufferedReader`与`BufferedWriter`实例,并确保即使发生异常也能自动关闭这两个流。接着进入循环逐行入并立即出至新位置直到遇到文件结尾为止。最后捕获可能发生的I/O错误以便于调试[^2]。 值得注意的是,在实际应用当中应当考虑更复杂的场景比如大文件分块加载或是并发访问等问题。此外,当涉及到其他类型的文件如二进制文件时,则应选用相应的字节流来进行类似的优化[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日居月诸Rijuyuezhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值