概念:
- 重载函数调用操作符的类,其对象常称为函数对象
- 函数对象使用重载()时,行为类似函数调用,也叫仿函数
本质:
函数对象(仿函数)是一个类,不是一个函数
特点:
- 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
- 函数对象超出普通函数的概念,函数对象可以有自己的状态
- 函数对象可以作为参数传递
示例:
#include谓词概念using namespace std; //函数对象 class MyAdd { public: int operator()(int num1, int num2) { return num1 + num2; } }; //1、函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值 void _01Test01() { MyAdd add; cout << add(10, 20) << endl;; } //2、函数对象超出普通函数的概念,函数对象可以有自己的状态 class MyPrint { public: //记录MyPrint被调用的次数 int count; public: MyPrint() { count = 0; } void operator()(string str) { cout << str << endl; count++; } }; void _01Test02() { MyPrint print; print("hello"); print("hello"); print("hello"); print("hello"); print("hello"); cout << "print被调用的次数:" << print.count << endl; } //3、函数对象可以作为参数传递 void doPrint(MyPrint& print, string str) { print(str); } void _01Test03() { MyPrint print; string str = "hello world"; doPrint(print, str); } void main() { //_01Test01(); //_01Test02(); _01Test03(); }
概念:
- 返回bool类型的仿函数叫做谓词
- 如果operator()接收一个参数,就叫做一元谓词
- 如果operator()接收两个参数,就叫做二元谓词
示例:
一元谓词:
#include#include #include using namespace std; //一元谓词 operator()参数只有一个 class GreaterFive { public: bool operator()(int num) { return num > 5; } }; void _Test02() { vector v; for (int i = 0; i < 10; i++) { v.push_back(i); } vector ::iterator it = find_if(v.begin(), v.end(), GreaterFive()); if (it != v.end()) { cout << *it << endl; } else { cout << "没有大于5的数" << endl; } } void main() { _Test02(); }
二元谓词:
#include内建函数对象#include #include using namespace std; //二元谓词 class MyCompare { public: bool operator()(int num1, int num2) { return num1 > num2; } }; void _03Test01() { vector v; v.push_back(10); v.push_back(40); v.push_back(30); v.push_back(50); v.push_back(20); sort(v.begin(), v.end()); for (vector ::iterator it = v.begin();it!= v.end(); it++) { cout << *it << endl; } cout << "------------------------" << endl; sort(v.begin(), v.end(), MyCompare()); for (vector ::iterator it = v.begin(); it!=v.end(); it++) { cout << *it << endl; } } void main() { _03Test01(); }
概念:
- STL内建了一些函数对象
分类:
- 算术仿函数
- 关系仿函数
- 逻辑仿函数
用法:
- 这些仿函数所产生的对象,用法和一般函数完全相同
- 使用内建函数对象,需要引入头文件#include
功能:
- 实现四则运算
- 其中negate是一元运算。其他的都是二元运算
仿函数原型:
- template < class T> T plus< T> //加法仿函数
- template < class T > T minus< T > //减法仿函数
- template < class T > T multiplies< T> //乘法仿函数
- template < class T > T divides< T> //除法仿函数
- template < class T > T modulus< T> //取模仿函数
- template < class T > T negate< T> //取反仿函数
示例:
#include关系仿函数#include using namespace std; //内建函数对象 算术仿函数 //取反仿函数 void _04Test01() { negate qufan; cout << qufan(50) << endl; } //加法仿函数 void _04Test02() { plus add; cout << add(10, 20) << endl; } //减法仿函数 void _04Test03() { minus min; cout << min(10, 20) << endl; } void main() { //_04Test01(); //_04Test02(); _04Test03(); }
功能:
- 实现关系对比
仿函数原型:
- template< class T > bool equal_to< T > //等于
- template< class T > bool not_equal_to< T > //不等于
- template< class T > bool greater< T > //大于
- template< class T > bool greater_equal< T > //大于等于
- template< class T > bool less< T > //小于
- template< class T > bool less_greater< T > //小于等于
功能:
- 实现逻辑运算
函数原型:
- template< class T > bool logical_and //逻辑与
- template< class T > bool logical_or //逻辑或
- template< class T > bool logical_not //逻辑非



