template<typename Predicate>
class my_unary_negate : public unary_function<typename Predicate::argument_type,typename Predicate::result_type> {
protected:
Predicate pred;
public:
my_unary_negate(const Predicate &x) : pred(x) {}
bool operator()(const typename Predicate::argument_type & x) {
return !pred(x);
}
};
template<typename Predicate>
inline my_unary_negate<Predicate>mynot1(const Predicate& pred) {
return my_unary_negate<Predicate>(pred);
}
用起来:
配合我写的C++bind2nd
int main()
{
vector<int>vt = {1,2,5,6,710,12,15};
sort(vt.begin(),vt.end(),myless<int>());
for(auto i : vt) cout<<i<<' ';
cout<< count_if(vt.begin(),vt.end(),not1(mybind2nd(less<int>(),40)));
//cout<< count_if(vt.begin(),vt.end(),mynot1(mybind2nd(less<int>(),40)));
return 0;
}
结果跟原版一致。