目录
组合适配器:not1、not2 函数指针适配器:ptr_fun
STL整体结构
仿函数(函数对象)
所谓的仿函数(functor),是通过重载()运算符模拟函数形为的类
函数对象
- 一个行为类似函数的对象
- 可以没有参数,也可以带有若干参数
- 其功能是获取一个值,或者改变操作的状态。
- 例
- 普通函数就是函数对象
- 重载了“()”运算符的类的实例是函数对象
C语言使用函数指针和回调函数来实现仿函数,例如一个用来排序的函数可以这样使用仿函数
#include <stdio.h> #include <stdlib.h> //int sort_function( const void *a, const void *b); int sort_function( const void *a, const void *b) { return *(int*)a-*(int*)b; } int main() { int list[5] = { 54, 21, 11, 67, 22 }; qsort((void *)list, 5, sizeof(list[0]), sort_function);//起始地址,个数,元素大小,回调函数 int x; for (x = 0; x < 5; x++) printf("%i\n", list[x]); return 0; }
函数对象概念图
STL提供的函数对象
- 用于算术运算的函数对象:
- 一元函数对象(一个参数) :negate
- 二元函数对象(两个参数) :plus、minus、multiplies、divides、modulus
- 用于关系运算、逻辑运算的函数对象(要求返回值为bool)
- 一元谓词(一个参数):logical_not
- 二元谓词(两个参数):equalto、notequalto、greater、less、greaterequal、lessequal、logicaland、logical_or
函数适配器
- 绑定适配器: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)”
绑定适配器:bind1st、bind2nd
- binder2nd的实例构造通常比较冗长,bind2nd函数用于辅助构造binder2nd,产生它的一个实例。
- binder1st和bind1st,将一个具体值绑定到二元函数的第一个参数。
//函数适配器实例——找到数组中第一个大于40的元素 #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)为真的元素。
组合适配器:not1、not2 函数指针适配器:ptr_fun
- 对于一般的逻辑运算,有时可能还需要对结果求一次逻辑反。
- unarynegate和binarynegate实现了这一适配功能。STL还提供了not1和not2辅助生成相应的函数对象实例,分别用于一元谓词和二元谓词的逻辑取反。
// ptr_fun、not1和not2产生函数适配器实例 #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; }
成员函数适配器:ptrfun、ptrfun_ref
//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; }