概念:
重载函数调用操作符的类 其对象常称为函数对象
函数对象使用重载的()时 行为类似函数调用 也叫仿函数
本质:
函数对象(仿函数)是一个类 不是一个函数
函数对象使用特点:
函数对象在使用时 可以像普通函数那样调用 可以有参数和返回值
函数对象超出了普通函数的概念 函数对象可以有自己的状态
函数对象可以作为参数传递
#includeusing namespace std; //函数对象在使用时 可以像普通函数那样调用 可以有参数和返回值 class MyAdd{ public: int operator()(int v1, int v2){ return v1 + v2; } }; void test01(){ MyAdd ad; cout << ad(10,10) << endl; } //函数对象超出了普通函数的概念 函数对象可以有自己的状态 class MyPrint{ public: MyPrint(){ this->count = 0; } void operator()(string test){ cout << test << endl; this->count++; } int count; //内部自己的状态 }; void test02(){ MyPrint mp; mp("hello world"); mp("c++"); //因为mp是一个类 所以可以通过count来记录自身的状态 //而普通函数不是一个类 必须通过静态成员或者全局变量来实现 cout << "MyPrint函数调用的次数为:" << mp.count << endl; } //函数对象可以作为参数传递 void doPrint(MyPrint &mp, string test){ mp(test); } void test03(){ MyPrint mp; doPrint(mp, "hello"); } int main() { test01(); test02(); test03(); return 0; }
输出结果:
谓词20
hello world
c++
MyPrint函数调用的次数为:2
hello
概念:
返回bool类型的仿函数称为谓词
如果operator()接受一个参数 那么叫做一元谓词
如果operator()接受两个参数 那么叫做二元谓词
一元谓词:
#includeusing namespace std; #include #include class GreaterFive{ public: bool operator()(int val) //一个参数 所以是一元谓词 { return val >5; } }; void test01(){ vector v; for(int i=0; i<10; i++) { v.push_back(i); } //查找容器中有没有大于5的数字 //GreaterFive() 匿名的函数对象 //find_if中的i第三个参数是_Pr_Pred 也就是谓词的意思 vector ::iterator it = find_if(v.begin(),v.end(),GreaterFive()); if(it == v.end()){ cout << "未找到" << endl; }else{ cout << "找到了第一个大于5的数字:" << *it << endl; } } int main() { test01(); return 0; }
输出结果:
找到了第一个大于5的数字:6
二元谓词:
#includeusing namespace std; #include #include class MyCompare{ public: //两个参数 所以是二元谓词 bool operator()(int val1, int val2) { return val1 > val2; } }; void printVec(vector &v){ for(vector ::iterator it = v.begin();it != v.end(); it++){ cout << *it << " "; } cout << endl; } void test01(){ vector v; v.push_back(10); v.push_back(50); v.push_back(20); v.push_back(15); v.push_back(30); //用标准算法sort排序 默认是升序 sort(v.begin(),v.end()); printVec(v); //使用仿函数改变算法策略 变为从大到小 sort(v.begin(),v.end(),MyCompare()); cout << "-------------------" << endl; printVec(v); } int main() { test01(); return 0; }
输出结果:
内建函数对象10 15 20 30 50
-------------------
50 30 20 15 10
概念:
STL内建了一些函数对象
分类:
算数仿函数
关系仿函数
逻辑仿函数
用法:
这些仿函数所产生的对象 用法和一般函数完全相同
使用内建函数对象 需要引入头文件 #include
算数仿函数:
功能:
实现四则运算
其中negate是一元运算 其他都是二元运算
仿函数原型:
template
template
template
template
template
template
#includeusing namespace std; #include void test01(){ //取反仿函数 negate n; cout << n(50) << endl; } void test02(){ //这里虽然相加是二元谓词 但是只传一个int就可以了 //因为加法要求两个数相加 数据类型要保持一致 plus p; cout << p(10,20) << endl; } //其他的减乘除和加法用法一样 不做演示 int main() { test01(); test02(); return 0; }
输出结果:
-50
30
关系仿函数:
功能描述:
实现关系对比
仿函数原型:
template
template
template
template
template
template
#includeusing namespace std; #include #include #include void printVec(vector &v){ for(vector ::iterator it = v.begin();it != v.end();it++){ cout << *it << " "; } cout << endl; } void test01(){ vector v; v.push_back(1); v.push_back(3); v.push_back(4); v.push_back(2); printVec(v); //使用内建的仿函数创建对象实现将序 sort(v.begin(),v.end(),greater ()); printVec(v); } //其他关系仿函数用法一样 int main() { test01(); return 0; }
输出结果:
1 3 4 2
4 3 2 1
逻辑仿函数:
功能描述:
实现逻辑运算 实际开发中 运用的很少
函数原型:
template
template
template
#includeusing namespace std; #include #include #include void printVec(vector &v){ for(vector ::iterator it = v.begin();it != v.end();it++){ cout << *it << " "; } cout << endl; } void test01(){ vector v; v.push_back(true); v.push_back(false); v.push_back(false); v.push_back(true); //true 和 false 本质上是1 和 0 printVec(v); vector v2; //将v2的空间扩展到和v一样的大小 v2.resize(v.size()); //利用逻辑非 将容器v搬运到 容器v2中 并执行取反操作 //参数解析:把v的区间begin到end 拷贝到从v2的begin从开始的地方 //并执行 logical_not transform(v.begin(),v.end(),v2.begin(),logical_not ()); printVec(v2); } //其他关系仿函数用法一样 int main() { test01(); return 0; }
输出结果:
暑期编程PK赛 得CSDN机械键盘等精美礼品!1 0 0 1
0 1 1 0



