- 一、函数对象
- 函数对象概念
- 函数对象使用
- 二、谓词
- 三、内建函数对象
概念:
1)重载函数调用操作符的类,其对象常称为函数对象
2)函数对象使用重载的()时,行为类似函数调用,也叫仿函数。
本质:
函数对象(仿函数)是一个类,不是一个函数。
特点:
1)函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值;
2)函数对象超出普通函数的概念,函数对象可以有自己的状态;
3)函数对象可以作为参数传递。
#include二、谓词using namespace std; #include // 1.函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值 class MyAdd { public: int operator()(int v1, int v2) { return v1 + v2; } }; void test01() { MyAdd myadd; cout << myadd(10, 10) << endl; } // 2.函数对象可以有自己的状态 class MyPrint { public: MyPrint() // 初始化 { count = 0; } void operator()(string test) { cout << test << endl; count++; // 统计使用次数 } int count; // 内部有自己的状态 }; void test02() { MyPrint mp; mp("hello C++"); mp("hello C++"); mp("hello C++"); cout << "mp的调用次数为:" << mp.count << endl; } // 3.函数对象可以作为参数传递 void doprint(MyPrint &mp, string test) { mp(test); } void test03() { MyPrint mp; doprint(mp, "hello c++"); } int main() { test03(); system("pause"); return 0; }
概念:
返回bool类型的仿函数称为谓词。
如果operator()接受一个参数,叫做一元谓词;
operator()接受两个参数,叫做二元谓词。
#include三、内建函数对象using namespace std; #include #include // 一元谓词:仿函数 class Greatfive { public: bool operator()(int val) { return val > 5; } }; void test01() { vector v; for (int i = 0;i < 10;i++) { v.push_back(i); } // 查找容器中大于5的数字 // Greatfive() 匿名函数对象 vector ::iterator it = find_if(v.begin(), v.end(), Greatfive()); if (it == v.end()) cout << "未找到" << endl; else cout << "找到:" << *it << endl; } // 二元谓词:仿函数 class Mycompare { public: bool operator()(int v1, int v2) { return v1 > v2; } }; void test02() { // 创建了一个vector容器 vector v; // 向容器中插入数据 v.push_back(10); v.push_back(40); v.push_back(20); v.push_back(50); v.push_back(30); // 默认从小到大排序 sort(v.begin(), v.end()); for (vector ::iterator it = v.begin();it != v.end();it++) { cout << *it << " "; } cout << endl; cout << "***************************" << endl; // 使用函数对象改变算法策略,降序排列 sort(v.begin(), v.end(),Mycompare()); for (vector ::iterator it = v.begin();it != v.end();it++) { cout << *it << " "; } cout << endl; } int main() { test01(); cout << "-------------------------" << endl; test02(); system("pause"); return 0; }
主要有三种:算术仿函数、关系仿函数、逻辑仿函数。
#includeusing namespace std; #include #include #include // 算术仿函数 // negate 一元仿函数,取反 void test01() { negate n; cout << n(50) << endl; } // plus 二元仿函数,加法 void test02() { plus p; cout << p(10,50) << endl; } // 关系仿函数 // 大于,greater void test03() { vector v; // 向容器中插入数据 v.push_back(10); v.push_back(40); v.push_back(20); v.push_back(50); v.push_back(30); // 默认从小到大排序 sort(v.begin(), v.end()); for (vector ::iterator it = v.begin();it != v.end();it++) { cout << *it << " "; } cout << endl; cout << "***************************" << endl; // 使用内建函数对象(大于仿函数)改变算法策略,降序排列 sort(v.begin(), v.end(),greater ()); for (vector ::iterator it = v.begin();it != v.end();it++) { cout << *it << " "; } cout << endl; } // 逻辑仿函数 // 逻辑非,logical_not void test04() { vector v1; // 向容器中插入数据 v1.push_back(true); v1.push_back(false); v1.push_back(true); v1.push_back(false); for (vector ::iterator it = v1.begin();it != v1.end();it++) { cout << *it << " "; } cout << endl; // 将v1容器的数据搬运到v2容器中,并执行逻辑非运算 vector v2; v2.resize(v1.size()); // 重新指定容器大小 transform(v1.begin(), v1.end(), v2.begin(),logical_not ()); for (vector ::iterator it = v2.begin();it != v2.end();it++) { cout << *it << " "; } cout << endl; } int main() { test04(); system("pause"); return 0; }
结束语
大家的点赞和关注是博主最大的动力,博主所有博文中的代码文件都可分享给您(除了少量付费资源),如果您想要获取博文中的完整代码文件,可通过C币或积分下载,没有C币或积分的朋友可在关注、点赞和评论博文后,私信发送您的邮箱,我会在第一时间发送给您。博主后面会有更多的分享,敬请关注哦!



