-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.5.cc
168 lines (139 loc) · 3.22 KB
/
16.5.cc
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
165
166
167
168
#include <unordered_set>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cstring>
using namespace std;
class A{
friend class std::hash<A>;
friend std::ostream & operator<<(std::ostream & os, const A & a);
public:
A():sold(0){}
A(unsigned n):sold(n){}
A(const A & a):sold(a.sold){}
bool operator==(const A & a) const {
return sold == a.sold;
}
private:
unsigned sold;
};
std::ostream & operator<<(std::ostream & os, const A & a){
os << a.sold;
return os;
}
namespace std{
template<>
struct hash<A>{
public:
typedef size_t result_type;
typedef A argument_type;
std::size_t operator()(const A & a) const;
};
std::size_t hash<A>::operator()(const A & a) const{
return hash<unsigned>()(a.sold);
}
}
void test(){
std::unordered_set<A> um;
um.insert(A());
um.insert(A());
um.insert(A(1));
for(auto & i : um){
std::cout << i << std::endl;
}
}
template<typename InputIterator, typename T>
size_t count(InputIterator b, InputIterator e, T v){
size_t ret = 0;
for(auto __first = b; __first != e; ++__first)
if(*__first == v)
++ret;
return ret;
}
template<typename T>
size_t count(vector<T> & vec, T val){
size_t ret = 0;
for(auto & elem : vec)
if(elem == val)
++ret;
return ret;
}
template<>
size_t count(vector<const char *> & ccvec, const char * val){
size_t ret = 0;
for(auto & p : ccvec)
if(!strcmp(p, val))
++ret;
return ret;
}
void test2(){
vector<int> ivec{1,2,3,45,781,1,3,45,2,121,1};
cout << count(ivec, 1) << endl;
vector<string> svec {"hello", "world", "hello", "hell", "hello"};
cout << count(svec, string("hell")) << endl;
}
void test3(){
vector<const char *> ccvec{"hello", "world", "hello", "hell"};
cout << count(ccvec, "hello") << endl;
}
template <typename T>
string debug_rep(const T & t){
ostringstream ret;
ret << t;
return ret.str();
}
template <typename T>
string debug_rep(T * t){
ostringstream ret;
ret << "pointer: " << t;
if(t)
ret << " " << *t;
else
ret << " nullptr";
return ret.str();
}
template<>
string debug_rep(const string & s){
return '"' + s + '"';
}
template<>
string debug_rep(const char * p){
return debug_rep(string(p));
}
/* 优先级最高 */
string debug_rep(const char * p){
return '"' + debug_rep(string(p)) + '"';
}
template<>
string debug_rep(char * p){
return debug_rep(string(p));
}
template<typename T>
ostream & print(ostream & os, const T & t){
os << t;
return os;
}
/* 必须按照这种格式递归
必须有结束的特例
*/
template<typename T, typename... Args>
ostream & print(ostream & os, const T & t, const Args&... rest)
{
os << t << endl;
print(os, rest...);
return os;
}
template<typename... Args>
ostream & errorMsg(ostream & os, Args &&... args){
return print(os, debug_rep(std::forward<Args>(args))...);
}
void test4(){
/* errorMsg => debug_rep 获得字符串 => print 打印 */
errorMsg(cout, "hello", "hello", "world", string("string"));
}
int main(int argc, char const *argv[])
{
test4();
return 0;
}