问题标题: 方便高效的cin/cout(基于cstdio)

2
2
已解决
薛乘志
薛乘志
初级启示者
初级启示者

使用:

将这段代码插到你代码的 #include<...> 和 using namespace std; 中间,确保 #include 了 cstdio 和 string(不要 #include <iostream>,否则编译报错别找我)

namespace std {
#define endl '\n'
	class stdio_input {
#define _input(T, F) stdio_input operator>>(T &data) {scanf(F, &data); stdio_input b; return b;}
		public:
			_input(bool, "%d");
			_input(int, "%d");
			_input(short, "%d");
			_input(long long, "%d");
			_input(unsigned int, "%u");
			_input(unsigned short, "%u");
			_input(unsigned long long, "%u");
			_input(float, "%f");
			_input(double, "%f");
			_input(char, "%c");
			_input(char*, "%s");
			stdio_input operator>>(std::string &data) {
				std::string str;
				char s = getchar();
				while (s == ' ' || s == '\n' || s == '\r') {
					s = getchar();
				}
				while (s != ' ' && s != '\n' && s != '\r') {
					str += s;
					s = getchar();
				}
				data = str;
				stdio_input b;
				return b;
			}
	};
	class stdio_output {
#define _output(T, F) stdio_output operator<<(T data) {printf(F, data); stdio_output b; return b;}
		public:
			_output(const bool, "%d");
			_output(const int, "%d");
			_output(const short, "%d");
			_output(const long long, "%d");
			_output(const unsigned int, "%u");
			_output(const unsigned short, "%u");
			_output(const unsigned long long, "%u");
			_output(const float, "%g");
			_output(const double, "%g");
			_output(const char, "%c");
			_output(const char*, "%s");
			_output(const void*, "%p");
			stdio_output operator<<(std::string data) {
				printf(data.c_str());
				stdio_output b;
				return b;
			}
	};
	stdio_input cin;
	stdio_output cout;
}

然后像正常写代码一样用cin和cout即可(懒人),如:

int main() {
	int n;
	float k;
	cin >> n >> k;
	cout << n * k << endl;
	string s1, s2;
	cin >> s1 >> s2;
	cout << s1 << " " << s2 << endl;
	int a[1005];
	cin >> a[0];
	cout << a[0] << " " << a << endl;
	return 0;
}

内部使用scanf/printf实现,效率几乎相等(所以可以给某些不愿使用printf的小游戏升级了)

薛乘志在2022-02-09 15:31:46追加了内容

完整使用代码:https://pastebin.ubuntu.com/p/NnYPMHjSvc/

薛乘志在2022-02-13 11:35:38追加了内容

更新(inline使效率更快)

#include <stdio.h>

namespace std {
#define endl '\n'
	class stdio_input {
#define _input(T, F) inline stdio_input operator>>(T &data) {scanf(F, &data); return *this;}
		public:
			_input(bool, "%d");
			_input(int, "%d");
			_input(short, "%d");
			_input(long long, "%d");
			_input(unsigned int, "%u");
			_input(unsigned short, "%u");
			_input(unsigned long long, "%u");
			_input(float, "%f");
			_input(double, "%f");
			_input(char, "%c");
			_input(char*, "%s");
#undef _input
	};
	class stdio_output {
#define _output(T, F) inline stdio_output operator<<(T data) {printf(F, data); return *this;}
		public:
			_output(const bool, "%d");
			_output(const int, "%d");
			_output(const short, "%d");
			_output(const long long, "%d");
			_output(const unsigned int, "%u");
			_output(const unsigned short, "%u");
			_output(const unsigned long long, "%u");
			_output(const float, "%g");
			_output(const double, "%g");
			_output(const char, "%c");
			_output(const char*, "%s");
			_output(const void*, "%p");
#undef _output
	};
	stdio_input cin;
	stdio_output cout;
}

 

薛乘志在2022-02-13 11:38:21追加了内容

再更新(不小心把string漏掉了)

//My iostream

#include <cstdio>
#include <string>

namespace std {
#define endl '\n'
	class stdio_input {
#define _input(T, F) inline stdio_input operator>>(T &data) {scanf(F, &data); return *this;}
		public:
			_input(bool, "%d");
			_input(int, "%d");
			_input(short, "%d");
			_input(long long, "%d");
			_input(unsigned int, "%u");
			_input(unsigned short, "%u");
			_input(unsigned long long, "%u");
			_input(float, "%f");
			_input(double, "%f");
			_input(char, "%c");
			_input(char*, "%s");
			inline stdio_input operator>>(std::string &data) {
				std::string str;
				char s = getchar();
				while (s == ' ' || s == '\n' || s == '\r') {
					s = getchar();
				}
				while (s != ' ' && s != '\n' && s != '\r') {
					str += s;
					s = getchar();
				}
				data = str;
				return *this;
			}
#undef _input
	};
	class stdio_output {
#define _output(T, F) inline stdio_output operator<<(T data) {printf(F, data); return *this;}
		public:
			_output(const bool, "%d");
			_output(const int, "%d");
			_output(const short, "%d");
			_output(const long long, "%d");
			_output(const unsigned int, "%u");
			_output(const unsigned short, "%u");
			_output(const unsigned long long, "%u");
			_output(const float, "%g");
			_output(const double, "%g");
			_output(const char, "%c");
			_output(const char*, "%s");
			_output(const void*, "%p");
			inline stdio_output operator<<(std::string data) {
				printf(data.c_str());
				return *this;
			}
#undef _output
	};
	stdio_input cin;
	stdio_output cout;
}

 


0
已采纳
包涵宇
包涵宇
中级天翼
中级天翼

手写快读不香吗

0
0
吴庞茂旭
吴庞茂旭
资深光能
资深光能

printf和scanf那么适用方便,为什么不用它们?

我写游戏基本上都是用printf,puts啊。

0
我要回答