-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.hpp
164 lines (127 loc) · 3.69 KB
/
binary.hpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef SJTU_FILEMANAGER_HPP
#define SJTU_FILEMANAGER_HPP
#include <fstream>
#include <cstring>
#include <iostream>
#include<vector>
#include<algorithm>
namespace sjtu {
using std::vector;
using std::pair;
using std::string;
using std::ifstream;
using std::ofstream;
class mystream{
public:
char* cur;
public:
void open(char *buff){
cur=buff;
}
void read(char* a,long len){
for(int i=0;i<len;i++){
*(a+i)=*cur;
cur++;
}
}
void write(char *a,long len){
for(int i=0;i<len;i++){
*cur=*(a+i);
cur++;
}
}
};
class InputOfBinary {
public:
mystream fin;
public:
void open(char *s){fin.open(s);}
friend InputOfBinary & operator >> (InputOfBinary & cin, int & A) {
cin.fin.read((char *) (& A), sizeof A);
return cin;
}
friend InputOfBinary & operator >> (InputOfBinary & cin, double & A) {
cin.fin.read((char *) (& A), sizeof A);
return cin;
}
friend InputOfBinary & operator >> (InputOfBinary & cin, char & A) {
cin.fin.read((char *) (& A), sizeof A);
return cin;
}
friend InputOfBinary & operator >> (InputOfBinary & cin, bool & A) {
cin.fin.read((char *) (& A), sizeof A);
return cin;
}
friend InputOfBinary & operator >> (InputOfBinary & cin, string & A) {
int len;
cin>>len;
A="";
for(int i=0;i<len;i++){
char c;cin>>c;
A+=c;
}
return cin;
}
template<class T1,class T2>
friend InputOfBinary & operator >> (InputOfBinary & cin, pair<T1,T2> & A) {
cin>>A.first>>A.second;
return cin;
}
template<class T>
friend InputOfBinary & operator >> (InputOfBinary & cin, vector<T> & A) {
int len;
cin>>len;
A.clear();
for(int i=0;i<len;i++){
T c;cin>>c;
A.push_back(c);
}
return cin;
}
~InputOfBinary() {
}
};
class OutputOfBinary {
public:
mystream fout;
public:
void open(char *s){fout.open(s);}
friend OutputOfBinary & operator << (OutputOfBinary & cout, const int & A) {
cout.fout.write((char *) (& A), sizeof A);
return cout;
}
friend OutputOfBinary & operator << (OutputOfBinary & cout, const double & A) {
cout.fout.write((char *) (& A), sizeof A);
return cout;
}
friend OutputOfBinary & operator << (OutputOfBinary & cout, const char & A) {
cout.fout.write((char *) (& A), sizeof A);
return cout;
}
friend OutputOfBinary & operator << (OutputOfBinary & cout, const bool & A) {
cout.fout.write((char *) (& A), sizeof A);
return cout;
}
friend OutputOfBinary & operator << (OutputOfBinary & cout, const string & A) {
cout<<(int)A.length();
for(int i=0;i<A.length();i++)
cout<<A[i];
return cout;
}
template<class T>
friend OutputOfBinary & operator << (OutputOfBinary & cout, const vector<T> & A) {
cout<<(int)A.size();
for(int i=0;i<A.size();i++)
cout<<(T)A.at(i);
return cout;
}
template<class T1,class T2>
friend OutputOfBinary & operator << (OutputOfBinary & cout, const pair<T1,T2> & A) {
cout<<(T1)A.first<<(T2)A.second;
return cout;
}
~OutputOfBinary() {
}
};
};
#endif