之前学的函数指针,指针函数,阿巴阿巴乱七八糟你是否不容易记忆且理解不了
所以后面C++优化,提出了仿函数
仿函数定义:就是在单独类里面重载()
#include<iostream>
using namespace std;
class Big_bool
{
public:
bool operator()(int a, int b) { return a > b; }
};
template<class T1,class T2>
class Small_bool
{
public:
bool operator()(T1 a, T2 b) { return a < b; }
};
int main()
{
Big_bool big_bool;
//先要申请这个类对象,函数本身是在类里,有了对象就能调用
cout << big_bool(2, 1) << endl;
cout << big_bool.operator()(2, 1) << endl;//展开后
Small_bool<double,double> small_bool;
cout << small_bool(2.2, 3.5) << endl;
return 0;
}