仿函数(函数对象)

xiaoxiao2021-02-28  42

函数对象

一个行为类似函数的对象可以没有参数,也可以带有若干参数其功能是获取一个值,或者改变操作的状态。例 普通函数就是函数对象重载了“()”运算符的类的实例是函数对象

函数对象概念图

例10-13、例10-14:

使用两种方式定义表示乘法的函数对象 通过定义普通函数(例10-13)通过重载类的“()”运算符(例10-14)

用到以下算法:

template<class InputIterator, class Type, class BinaryFunction> Type accumulate(InputIterator first, InputIterator last, Type val, BinaryFunction binaryOp); 对[first, last)区间内的数据进行累“加”,binaryOp为用二元函数对象表示的“加”运算符,val为累“加”的初值

例10-13

#include <iostream> #include <numeric> //包含数值算法头文件 using namespace std; //定义一个普通函数 int mult(int x, int y) { return x * y; }; int main() { int a[] = { 1, 2, 3, 4, 5 }; const int N = sizeof(a) / sizeof(int); cout << "The result by multipling all elements in a is " << accumulate(a, a + N, 1, mult) << endl; return 0; }

例10-14

//10_14.cpp #include <iostream> #include <numeric> //包含数值算法头文件 using namespace std; class MultClass{ //定义MultClass类 public: //重载操作符operator() int operator() (int x, int y) const { return x * y; } }; int main() { int a[] = { 1, 2, 3, 4, 5 }; const int N = sizeof(a) / sizeof(int); cout << "The result by multipling all elements in a is " << accumulate(a, a + N, 1, MultClass()) //将类multclass传递给通用算法 << endl; return 0; }

STL提供的函数对象

用于算术运算的函数对象: 一元函数对象(一个参数) :negate二元函数对象(两个参数) :plus、minus、multiplies、divides、modulus用于关系运算、逻辑运算的函数对象(要求返回值为bool) 一元谓词(一个参数):logical_not二元谓词(两个参数):equalto、notequalto、greater、less、greaterequal、lessequal、logicaland、logical_or

例10-15 利用STL标准函数对象

//10_15.cpp #include <iostream> #include <numeric> //包含数值算法头文件 #include <functional> //包含标准函数对象头文件 using namespace std; int main() { int a[] = { 1, 2, 3, 4, 5 }; const int N = sizeof(a) / sizeof(int); cout << "The result by multipling all elements in A is “ << accumulate(a, a + N, 1, multiplies<int>()) << endl; //将标准函数对象传递给通用算法 return 0; }

例10-16利用STL中的二元谓词函数对象

// 10_16.cpp #include <functional> #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int intArr[] = { 30, 90, 10, 40, 70, 50, 20, 80 }; const int N = sizeof(intArr) / sizeof(int); vector<int> a(intArr, intArr + N); cout << "before sorting:" << endl; copy(a.begin(),a.end(),ostream_iterator<int>(cout,"\t")); cout << endl; sort(a.begin(), a.end(), greater<int>()); cout << "after sorting:" << endl; copy(a.begin(),a.end(),ostream_iterator<int>(cout,"\t")); cout << endl; return 0; }

函数适配器

绑定适配器:bind1st、bind2nd 将n元函数对象的指定参数绑定为一个常数,得到n-1元函数对象组合适配器:not1、not2 将指定谓词的结果取反函数指针适配器:ptr_fun 将一般函数指针转换为函数对象,使之能够作为其它函数适配器的输入。在进行参数绑定或其他转换的时候,通常需要函数对象的类型信息,例如bind1st和bind2nd要求函数对象必须继承于binary_function类型。但如果传入的是函数指针形式的函数对象,则无法获得函数对象的类型信息。成员函数适配器:ptrfun、ptrfun_ref 对成员函数指针使用,把n元成员函数适配为n + 1元函数对象,该函数对象的第一个参数为调用该成员函数时的目的对象也就是需要将“object->method()”转为“method(object)”形式。将“object->method(arg1)”转为二元函数“method(object, arg1)”。

绑定适配器

binder2nd的实例构造通常比较冗长,bind2nd函数用于辅助构造binder2nd,产生它的一个实例。binder1st和bind1st,将一个具体值绑定到二元函数的第一个参数。

例10-17:函数适配器实例——找到数组中第一个大于40的元素

//10_17.cpp #include <functional> #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int intArr[] = { 30, 90, 10, 40, 70, 50, 20, 80 }; const int N = sizeof(intArr) / sizeof(int); vector<int> a(intArr, intArr + N); vector<int>::iterator p = find_if(a.begin(), a.end(), bind2nd(greater<int>(), 40)); if (p == a.end()) cout << "no element greater than 40" << endl; else cout << "first element greater than 40 is: " << *p << endl; return 0; } 注: find_if算法在STL中的原型声明为: template<class InputIterator, class UnaryPredicate> InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred); 它的功能是查找数组[first, last)区间中第一个pred(x)为真的元素。

组合适配器

对于一般的逻辑运算,有时可能还需要对结果求一次逻辑反。unarynegate和binarynegate实现了这一适配功能。STL还提供了not1和not2辅助生成相应的函数对象实例,分别用于一元谓词和二元谓词的逻辑取反。

例10-18 ptr_fun、not1和not2产生函数适配器实例

// 10_18.cpp #include <functional> #include<iostream> #include<vector> #include<algorithm> using namespace std; bool g(int x, int y) { return x > y; } int main() { int intArr[] = { 30, 90, 10, 40, 70, 50, 20, 80 }; const int N = sizeof(intArr) / sizeof(int); vector<int> a(intArr, intArr + N); vector<int>::iterator p; p = find_if(a.begin(), a.end(), bind2nd(ptr_fun(g), 40)); if (p == a.end()) cout << "no element greater than 40" << endl; else cout << "first element greater than 40 is: " << *p << endl; p = find_if(a.begin(), a.end(), not1(bind2nd(greater<int>(), 15))); if (p == a.end()) cout << "no element is not greater than 15" << endl; else cout << "first element that is not greater than 15 is: " << *p << endl; p = find_if(a.begin(), a.end(), bind2nd(not2(greater<int>()), 15)); if (p == a.end()) cout << "no element is not greater than 15" << endl; else cout << "first element that is not greater than 15 is: " << *p << endl; return 0; }

例10-19 成员函数适配器实例

//10_19.cpp #include <functional> #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Car { int id; Car(int id) { this->id = id; } void display() const { cout << "car " << id << endl; } }; int main() { vector<Car *> pcars; vector<Car> cars; for (int i = 0; i < 5; i++) pcars.push_back(new Car(i)); for (int i = 5; i < 10; i++) cars.push_back(Car(i)); cout << "elements in pcars: " << endl; for_each(pcars.begin(), pcars.end(), std::mem_fun(&Car::display)); cout << endl; cout << "elements in cars: " << endl; for_each(cars.begin(), cars.end(), std::mem_fun_ref(&Car::display)); cout << endl; for (size_t i = 0; i < pcars.size(); ++i) delete pcars[i]; return 0; }
转载请注明原文地址: https://www.6miu.com/read-2625567.html

最新回复(0)