函数适配器

来源:互联网 发布:个人信息管理系统源码 编辑:程序博客网 时间:2024/05/16 01:53

函数适配器

  • 绑定适配器: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)”。

1.绑定适配器

  • 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;elsecout << "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)为真的元素。
2.组合适配器
  • 对于一般的逻辑运算,有时可能还需要对结果求一次逻辑反。
  • 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;elsecout << "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;elsecout << "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;elsecout << "first element that is not greater than 15 is: " << *p << endl;return 0;}
运行结果:


3.成员函数适配器实例
#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;}

运行结果:

来自清华大学MOOC课件
0 0
原创粉丝点击