Part10 泛型程序设计与C++标准模板库 10.6函数对象
1函数对象
一个行为类似函数的对象
可以没有参数,也可以带有若干参数
其功能是获取一个值,或者改变操作的状态。
例
普通函数就是函数对象
重载了“()”运算符的类的实例是函数对象
函数对象概念图
使用两种方式定义表示乘法的函数对象
通过定义普通函数(例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 #include<iostream> #include<numeric> using namespace std; class MultClass{ public: int operator()(int x, int y) const {return x * y;} //重载操作符operator() }; 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()) << 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标准函数对象 #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中的二元谓词函数对象 #include<iostream> #include<functional> #include<vector> #include<iterator> #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; }
2函数适配器
绑定适配器: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的元素 #include<iostream> #include<vector> #include<functional> #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产生函数适配器实例 #include<iostream> #inlcude<functional> #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 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 成员函数适配器实例 #include<iostream> #include<vector> #include<algorithm> #include<functional> 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; }