新手如何做英文网站赚美元,一个app下载免费下载安装,百度用户服务中心官网,网站建设如何把代码目录
1#xff0c; 什么是函数对象
2#xff0c; 创建函数对象
3#xff0c; 函数对象的特点
2-1#xff0c; 像普通函数使用
2-2#xff0c; 记录调用次数
2-3#xff0c; 当着参数传递
3#xff0c; 内建函数对象
3-1#xff0c; 算数仿函数
3-2#xff0c…目录
1 什么是函数对象
2 创建函数对象
3 函数对象的特点
2-1 像普通函数使用
2-2 记录调用次数
2-3 当着参数传递
3 内建函数对象
3-1 算数仿函数
3-2 逻辑仿函数
3-3 比较仿函数 1 什么是函数对象
重载函数调用操作符的类 由该类创建的对象叫着函数对象
2 创建函数对象
//该类中创建了重载函数调用符的方法
class MyClass
{
public:int operator()(int a, int b){return a b;}
};int main()
{//m就是函数对象MyClass m;system(pause);return 0;
}
3 函数对象的特点
2-1 像普通函数使用
//像函数一样使用所以叫仿函数
m(10, 10);
2-2 记录调用次数
class MyClass
{
public:int operator()(int a, int b){//记录调用次数m_count;return a * b;}int m_count 0;
};
2-3 当着参数传递
void test2(MyClass m, int a, int b)
{cout m(a, b) endl;
}void test1()
{MyClass m;test2(m, 10, 20);
}
3 内建函数对象
内建函数对象需要包含头文件#include functional
3-1 算数仿函数
negateint n;
//取反后输出-10
cout n(10) endl;
//加法
plusint add;
cout add(10, 20) endl;
//减法
minusint m1;
cout m1(100, 10) endl;
//剩法
multipliesint m2;
cout m2(10, 2) endl;
//除法
dividesint d;
cout d(10, 3) endl;
//取模
modulusint m3;
cout m3(10, 3) endl;
3-2 逻辑仿函数
//大于
greaterint g1;
cout g1(10, 20) endl;
//大于等于
greater_equalint g2;
cout g2(10, 10) endl;
//小于
lessint g3;
cout g3(10, 20) endl;
//小于等于
less_equalint g4;
cout g4(10, 20) endl;
//等于
equal_toint e1;
cout e1(10, 20) endl;
//不等于
not_equal_toint e2;
cout e2(10, 20) endl;
3-3 比较仿函数
//与
logical_andint l1;
cout l1(1, 1) endl;
//或
logical_orint l2;
cout l2(0, 1) endl;
//非
logical_notint l3;
cout l3(1) endl;