-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
91 lines (79 loc) · 2.05 KB
/
Parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;
class InParser {
vector<char> str;
int ptr;
istream *fin;
char getChar() {
if (ptr == int(str.size())) {
fin->read(str.data(), str.size());
ptr = 0;
}
return str[ptr++];
}
template<class T>
T getInt() {
char chr = getChar();
while (!isdigit(chr) && chr != '-')
chr = getChar();
int sgn = +1;
if (chr == '-') {
sgn = -1;
chr = getChar();
}
T num = 0;
while (isdigit(chr)) {
num = num * 10 + chr - '0';
chr = getChar();
}
return sgn * num;
}
public:
InParser() : str(1e5), ptr(str.size()), fin(&cin) { }
InParser(const char* name) : str(1e5), ptr(str.size()), fin(&(*(new ifstream(name)))) { }
template<class T>
friend InParser& operator>>(InParser& in, T& num) {
num = in.getInt<T>();
return in;
}
};
class OutParser {
vector<char> str;
int ptr;
ostream *fout;
void putChar(char chr) {
if (ptr == int(str.size())) {
fout->write(str.data(), str.size());
ptr = 0;
}
str[ptr++] = chr;
}
template<class T>
void putInt(T num) {
if (num < 0) {
putChar('-');
num *= -1;
}
if (num > 9)
putInt(num / 10);
putChar(num % 10 + '0');
}
public:
OutParser() : str(1e5), ptr(0), fout(&cout) { }
OutParser(const char* name) : str(1e5), ptr(0), fout(&(*(new ofstream(name)))) { }
~OutParser() { fout->write(str.data(), ptr); }
template<class T>
friend OutParser& operator<<(OutParser& out, const T num) {
out.putInt(num);
return out;
}
friend OutParser& operator<<(OutParser& out, const char chr) {
out.putChar(chr);
return out;
}
friend OutParser& operator<<(OutParser& out, const char* str) {
for (int i = 0; str[i]; i++)
out.putChar(str[i]);
return out;
}
};