-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.h
84 lines (83 loc) · 1.87 KB
/
String.h
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
template< size_t N >
class String {
char c[N];
int the_size;
public:
bool operator<(const String<N> &s) const{
return strcmp(c, s.c) < 0;
}
bool operator==(const String<N> &s) const{
return strcmp(c, s.c) == 0;
}
String() {
the_size = 0;
}
String(char ch) {
c[0] = ch;
the_size = 1;
c[1] = 0;
}
String(string &s) {
strcpy(c, s.c_str());
the_size = s.size();
}
char* c_str(int idx = 0) {
return c + idx;
}
const char* c_str(int idx = 0) const {
return c + idx;
}
char& operator[](int idx) {
return c[idx];
}
int size() {
return the_size;
}
int input(char *&s) {
while (*s == ' ')
++s;
if (*s == 0)
return 0;
for (the_size = 0; (c[the_size] = *s) && isalphnum(c[the_size]); ++s)
++the_size;
c[the_size] = 0;
return 1;
}
int input_ex(char *&s) {
while (*s == ' ')
++s;
if (*s == 0)
return 0;
for (the_size = 0; (c[the_size] = *s) && (isalphnum(c[the_size]) || c[the_size] == '['); ++s)
++the_size;
c[the_size] = 0;
return 1;
}
int input_temp(char *s) {
while (*s == ' ')
++s;
if (*s == 0)
return 0;
for (the_size = 0; (c[the_size] = *s) && c[the_size] != ' '; ++s)
++the_size;
c[the_size] = 0;
return 1;
}
size_t hash() const{
size_t rt = 0;
for (int i = 0; i < the_size; ++i)
rt = ((rt << 5) + rt + c[i]);
return rt;
}
void print() {
printf("%s", c);
}
};
namespace std {
template<size_t N>
struct hash<String<N>> {
size_t operator()(const String<N> &s) const {
return hash<int>()(s.hash());
}
};
}