-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
316 lines (280 loc) · 6.96 KB
/
main.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <cstdlib>
#include <xstring>
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
template<typename _Arg, typename _Result>
struct my_unary_function
{
typedef _Arg argument_type;
typedef _Result result_type;
};
template<typename _Arg1, typename _Arg2, typename _Result>
struct my_binary_function
{
typedef _Arg1 first_argument_type;
typedef _Arg2 second_argument_type;
typedef _Result result_type;
};
template<typename _Ty>
struct plus : public my_unary_function<_Ty, _Ty>
{
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left + _Right;
}
};
template<typename _Ty>
struct minus : public my_unary_function<_Ty, _Ty>
{
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left - _Right;
}
};
template<typename _Ty>
struct multiplies : public my_unary_function<_Ty, _Ty>
{
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left * _Right;
}
};
template<typename _Ty>
struct divides : public my_unary_function<_Ty, _Ty>
{
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left / _Right;
}
};
template<typename _Ty>
struct modulus : public my_unary_function<_Ty, _Ty>
{
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left % _Right;
}
};
template<typename _Ty>
struct negate : public my_unary_function<_Ty, _Ty>
{
_Ty operator()(const _Ty& _Left) const
{
return ~_Left;
}
};
template<typename _Ty>
struct equal_to : public my_binary_function<_Ty, _Ty, bool>
{
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left == _Right;
}
};
template<typename _Ty>
struct not_equal_to : public my_binary_function<_Ty, _Ty, bool>
{
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left != _Right;
}
};
template<typename _Ty>
struct greater : public my_binary_function<_Ty, _Ty, bool>
{
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left > _Right;
}
};
template<typename _Ty>
struct less : public my_binary_function<_Ty, _Ty, bool>
{
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left < _Right;
}
};
template<typename _Ty>
struct greater_equal : public my_binary_function<_Ty, _Ty, bool>
{
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left >= _Right;
}
};
template<typename _Ty>
struct less_equal : public my_binary_function<_Ty, _Ty, bool>
{
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left <= _Right;
}
};
template<typename _Ty>
struct logical_and : public my_binary_function<_Ty, _Ty, bool>
{
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left && _Right;
}
};
template<typename _Ty>
struct logical_or : public my_binary_function<_Ty, _Ty, bool>
{
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left || _Right;
}
};
template<typename _Ty>
struct logical_not : public my_unary_function<_Ty, bool>
{
bool operator()(const _Ty& _Left) const
{
return !_Left;
}
};
template<typename _Fn1>
class unary_negate : public my_unary_function<typename _Fn1::argument_type, bool>
{
public:
explicit unary_negate(const _Fn1& _Func):_Functor(_Func){}
bool operator()(const typename _Fn1::argument_type _Left) const
{
return !_Functor(_Left);
}
private:
_Fn1 _Functor;
};
template<typename _Fn1>
inline unary_negate<_Fn1> not1(const _Fn1& _Func)
{
return unary_negate<_Fn1>(_Func);
}
template<typename _Fn2>
class binary_negate
: public my_binary_function<typename _Fn2::first_argument_type,
typename _Fn2::second_argument_type, bool>
{
public:
explicit binary_negate(const _Fn2& _Func):_Functor(_Func){}
bool operator()(const typename _Fn2::first_argument_type _Left,
const typename _Fn2::second_argument_type _Right) const
{
return !_Functor(_Left, _Right);
}
private:
_Fn2 _Functor;
};
template<typename _Fn2>
inline binary_negate<_Fn2> not2(const _Fn2& _Func)
{
return binary_negate<_Fn2>(_Func);
}
template<typename _Fn2>
class binder1st : public my_unary_function<typename _Fn2::second_argument_type,
typename _Fn2::result_type>
{
public:
typedef my_unary_function<typename _Fn2::second_argument_type,
typename _Fn2::result_type> _Base;
typedef typename _Base::argument_type argument_type;
typedef typename _Base::result_type result_type;
explicit binder1st(const _Fn2& _Func,
const typename _Fn2::first_argument_type& _Left)
:op(_Func), value(_Left){}
result_type operator()(const argument_type& _Right)
{
return op(value, _Right);
}
result_type operator()(const argument_type& _Right) const
{
return op(value, _Right);
}
private:
_Fn2 op;
typename _Fn2::first_argument_type value;
};
template<typename _Fn2, typename _Ty>
inline binder1st<_Fn2>
bind1st(const _Fn2& _Func, const _Ty& _Left)
{
typename _Fn2::first_argument_type _Val(_Left);
return binder1st<_Fn2>(_Func, _Val);
}
template<typename _Fn2>
class binder2st : public my_unary_function<typename _Fn2::first_argument_type,
typename _Fn2::result_type>
{
public:
typedef my_unary_function<typename _Fn2::first_argument_type,
typename _Fn2::result_type> _Base;
typedef typename _Base::argument_type argument_type;
typedef typename _Base::result_type result_type;
explicit binder2st(const typename _Fn2::second_argument_type& _Right,
const _Fn2& _Func)
:_Functor(_Func), value(_Right){}
result_type operator()(const argument_type& _Left)
{
return op(value, _Left);
}
result_type operator()(const argument_type& _Left) const
{
return op(value, _Left);
}
private:
_Fn2 op;
typename _Fn2::second_argument_type value;
};
template<typename _Fn2, class _Ty>
inline binder2st<_Fn2> bind2st(const _Fn2& _Func, const _Ty& _Right)
{
typename _Fn2::second_argument_type& _Val(_Right);
return binder2st<_Fn2>(_Right,_Func);
}
template<typename Iterator, typename _Fn2>
void my_sort(const Iterator& first, const Iterator& last, const _Fn2& func)
{
Iterator i,j;
int k = 0;
Iterator::value_type temp;
for(i = first; i < last - 1; ++i, ++k)
{
for(j = first; j < last - 1 - k; ++j)
{
if(func(*j, *(j + 1)))
{
temp = *j;
*j = *(j + 1);
*(j + 1) = temp;
}
}
}
}
template<typename Iterator, typename _Fn2>
Iterator my_find_if(Iterator& first, Iterator& last, const _Fn2& _func)
{
for(; first != last; ++first)
{
if(_func(*first))
{
return first;
}
}
return last;
}
int main()
{
vector<int> vec;
for(int i = 0; i < 10; ++i)
{
vec.push_back(rand() % 100 + 1);
}
my_sort(vec.begin(), vec.end(), greater<int>());
vector<int>::iterator it = my_find_if(vec.begin(), vec.end(),
not1(bind1st(greater<int>(), 50)));
cout << *it << endl;
}