概念:
- 重载函数操作符的类,其对象常称为函数对象
- 函数对象使用重载的()时,行为类似函数调用,也叫仿函数
本质:函数对象(仿函数)是一个类,不是一个函数
函数对象使用
- 特点
- 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
- 函数对象超出普通函数的概念,函数对象可以有自己的状态
- 函数对象可以作为参数传递
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <time.h>
using namespace std;
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
class MyPrint
{
public:
MyPrint()
{
this->count = 0;
}
void operator()(string test)
{
cout << test << endl;
this->count++;
}
int count; //记录调用次数
};
void doPrint(MyPrint& mp, string test)
{
mp(test);
}
void test()
{
MyAdd myadd;
cout << myadd(10, 10) << endl;
MyPrint myprint;
myprint("hello world");
myprint("hello world");
myprint("hello world");
myprint("hello world");
cout << "myprint调用次数:" << myprint.count << endl;
doPrint(myprint, "hello world!");
cout << "myprint调用次数:" << myprint.count << endl;
}
int main()
{
test();
return 0;
}
谓词
- 概念
- 返回bool类型的仿函数称为谓词
- 如果operator()接受一个参数,那么叫做一元谓词
-
class Five { public: bool operator()(int val) { return val > 5; } }; void test() { vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } //查找容器中有没有大于5的数字 //Five() 匿名函数对象 vector<int>::iterator it = find_if(v.begin(), v.end(), Five()); if (it == v.end()) { cout << "未找到" << endl; } else { cout << "找到了:" << *it << endl; } }
- 如果operator()接受两个参数,那么叫做二元谓词
-
class MySort { public: bool operator()(int v1, int v2) { return v1 > v2; } }; void test() { vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } sort(v.begin(), v.end(), MySort()); for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << endl; } }
内建函数对象 (STL内建的一些函数对象)
- 用法
- 这些仿函数所产生的对象,用法和一般函数完全相同
- 使用内建函数对象,需引入头文件#include <functional>
- 分类
- 算数仿函数(实现四则运算,其中negate是一元运算,其他都是二元运算)
- template<class T> T puls<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> //取反仿函数
-
void test() { negate<int> n; cout << n(50) << endl; plus<int> p; cout << p(10, 20) << endl; }
- 关系仿函数(实现关系对比)
- 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_equal<T> //........
-
void test() { vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } sort(v.begin(),v.end(), greater<int>()); for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << endl; } }
- 逻辑仿函数(实现逻辑运算)
- template<class T> bool logical_and<T> //与 (同时为真则为真)
- template<class T> bool logical_or<T> //或 (同时为假则为假)
- template<class T> bool logical_not<T> //非 (取反)
-
void test() { vector<bool> v; v.push_back(true); v.push_back(false); for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << " "; } cout << endl; vector<bool> v1; v1.resize(v.size()); //将v中的数据搬运到v1中,并取反 transform(v.begin(), v.end(), v1.begin(), logical_not<bool>()); for (vector<bool>::iterator it = v1.begin(); it != v1.end(); it++) { cout << *it << " "; } cout << endl; }
- 算数仿函数(实现四则运算,其中negate是一元运算,其他都是二元运算)