Skip to content

函数对象

重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象,也叫仿函数(functor),其实就是重载“()”操作符,使得类对象可以像函数那样调用。

把有operator()小括号运算符重载函数的对象,称作函数对象或者称作仿函数。如

cpp
class Sum{
 public:
 int operator()(int a, int b){ return a+b; }
};

Sum sum;
int ret = sum(10, 20);

在上述代码示例中,sum对象就是函数对象。

注意

  1. 函数对象(仿函数)是一个类,不是一个函数。
  2. 函数对象(仿函数)重载了”() ”操作符使得它可以像函数一样调用。

仿函数分类

假定某个类有一个重载的operator(),而且重载的operator()要求获取一个参数,我们就将这个类称为“一元仿函数”(unary functor);相反,如果重载的operator()要求获取两个参数,就将这个类称为“二元仿函数”(binary functor)

函数对象的作用

STL提供的算法往往都有两个版本,其中一个版本表现出最常用的某种运算,另一版本则允许用户通过template参数的形式来指定所要采取的策略。

cpp
//函数对象是重载了函数调用符号的类
class MyPrint{
public:
    MyPrint(){
        m_Num = 0;
    }
    int m_Num;

public:
    void operator() (int num){
        cout << num << endl;
        m_Num++;
    }
};

//函数对象
//重载了()操作符的类实例化的对象,可以像普通函数那样调用,可以有参数 ,可以有返回值
void test01() {
    MyPrint myPrint;
    myPrint(20);

}
// 函数对象超出了普通函数的概念,可以保存函数的调用状态
void test02(){
    MyPrint myPrint;
    myPrint(20);
    myPrint(20);
    myPrint(20);
    cout << myPrint.m_Num << endl;
}

void doBusiness(MyPrint print,int num){
    print(num);
}

//函数对象作为参数
void test03(){
    //参数1:匿名函数对象
    doBusiness(MyPrint(),30);
}

使用场景

利用函数对象指定set/priority_queue对象的排序规则

我们默认使用set容器的时间,如果只指定了容器中元素的类型,那么定义出来的容器 其中的元素都是按照从小到大进行排序的。但是可以通过指定函数对象来改变容器中元素的排序规则。

示例代码
cpp
#include <cstdlib>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
 // set<int> s;  // 默认其中元素从小到大进行排列
 set<int, greater<int>> s;// 通过指定函数对象影响元素排序规则
 for (int i = 0; i < 10; ++i) {
     s.insert(rand() % 100 + 1);
 }
 cout << "set:";
 for (auto i: s) {
     cout << i << " ";
 }
 cout << endl;

 //priority_queue<int> q; // 默认其中元素从大到小进行排列
 priority_queue<int, vector<int>, greater<int>> q;
 for (int i = 0; i < 10; ++i) {
     q.push(rand() % 100 + 1);
 }
 cout << "priority_queue:";
 while (!q.empty()) {
     cout << q.top() << " ";
     q.pop();
 }
 cout << endl;

 return 0;
}

编译运行后:

shell
$ ./a.out 
set:94 93 87 84 78 50 36 22 16 
priority_queue:27 27 28 37 41 60 63 64 73 91

谓词

谓词是指普通函数重载的operator()返回值是bool类型的函数对象(仿函数)。如果operator接受一个参数,那么叫做一元谓词,如果接受两个参数,那么叫做二元谓词,谓词可作为一个判断式。

cpp
class GreaterThenFive{
public:
    bool operator()(int num){
        return num > 5;
    }

};
//一元谓词
void test01(){
    vector<int> v;
    for (int i = 0; i < 10;i ++){
        v.push_back(i);
    }
    
     vector<int>::iterator it =  find_if(v.begin(), v.end(), GreaterThenFive());
     if (it == v.end()){
         cout << "没有找到" << endl;
     }
     else{
         cout << "找到了: " << *it << endl;
     }
}

//二元谓词
class MyCompare{
public:
    bool operator()(int num1, int num2){
        return num1 > num2;
    }
};

void test02(){
    vector<int> v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(20);
    v.push_back(90);
    v.push_back(60);

    //默认从小到大
    sort(v.begin(), v.end());
    for (vector<int>::iterator it = v.begin(); it != v.end();it++){
        cout << *it << " ";
    }
    cout << endl;
    cout << "----------------------------" << endl;
    //使用函数对象改变算法策略,排序从大到小
    sort(v.begin(), v.end(),MyCompare());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}

总结

  1. 函数对象通常不定义构造函数和析构函数,所以在构造和析构时不会发生任何问题,避免了函数调用的运行时问题。
  2. 函数对象超出普通函数的概念,函数对象可以有自己的状态
  3. 函数对象可内联编译,性能好。用函数指针几乎不可能
  4. 模版函数对象使函数对象具有通用性,这也是它的优势之一

内建函数对象

STL内建了一些函数对象。分为:算数类函数对象、关系运算类函数对象、逻辑运算类仿函数。这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以产生无名的临时对象来履行函数功能。使用内建函数对象,需要引入头文件 #include <functional>

  • 6个算数类函数对象,除了negate是一元运算,其他都是二元运算。
cpp
template<class T> T plus<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>//取反仿函数
  • 6个关系运算类函数对象,每一种都是二元运算。
cpp
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>//小于等于
  • 逻辑运算类运算函数,not为一元运算,其余为二元运算。
cpp
template<class T> bool logical_and<T>//逻辑与
template<class T> bool logical_or<T>//逻辑或
template<class T> bool logical_not<T>//逻辑非
内建函数对象示例代码
cpp
//取反仿函数
void test01()
{
    negate<int> n;
    cout << n(50) << endl;
}

//加法仿函数
void test02()
{
    plus<int> p;
    cout << p(10, 20) << endl;
}

//大于仿函数
void test03()
{
    vector<int> v;
    srand((unsigned int)time(NULL));
    for (int i = 0; i < 10; i++){
        v.push_back(rand() % 100);
    }

    for (vector<int>::iterator it = v.begin(); it != v.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
    sort(v.begin(), v.end(), greater<int>());

    for (vector<int>::iterator it = v.begin(); it != v.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}

函数对象适配器(绑定器)

TODO:

cpp
//函数适配器bind1st bind2nd
//现在我有这个需求 在遍历容器的时候,我希望将容器中的值全部加上100之后显示出来,怎么做?
//我们直接给函数对象绑定参数 编译阶段就会报错
//for_each(v.begin(), v.end(), bind2nd(myprint(),100));
//如果我们想使用绑定适配器,需要我们自己的函数对象继承binary_function 或者 unary_function
//根据我们函数对象是一元函数对象 还是二元函数对象
class MyPrint :public binary_function<int,int,void>
{
public:
    void operator()(int v1,int v2) const
    {
        cout << "v1 = : " << v1 << " v2 = :" <<v2  << " v1+v2 = :" << (v1 + v2) << endl;    
    }
};
//1、函数适配器
void test01()
{
    vector<int>v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    cout << "请输入起始值:" << endl;
    int x;
    cin >> x;

    for_each(v.begin(), v.end(), bind1st(MyPrint(), x));
    //for_each(v.begin(), v.end(), bind2nd( MyPrint(),x ));
}
//总结:  bind1st和bind2nd区别?
//bind1st : 将参数绑定为函数对象的第一个参数
//bind2nd : 将参数绑定为函数对象的第二个参数
//bind1st bind2nd将二元函数对象转为一元函数对象


class GreaterThenFive:public unary_function<int,bool>
{
public:
    bool operator ()(int v) const
    {
        return v > 5;
    }
};

//2、取反适配器
void test02()
{
    vector <int> v;
    for (int i = 0; i < 10;i++)
    {
        v.push_back(i);
    }
    
//  vector<int>::iterator it =  find_if(v.begin(), v.end(), GreaterThenFive()); //返回第一个大于5的迭代器
//  vector<int>::iterator it = find_if(v.begin(), v.end(),  not1(GreaterThenFive())); //返回第一个小于5迭代器
    //自定义输入
    vector<int>::iterator it = find_if(v.begin(), v.end(), not1 ( bind2nd(greater<int>(),5)));
    if (it == v.end())
    {
        cout << "没找到" << endl;
    }
    else
    {
        cout << "找到" << *it << endl;
    }

    //排序  二元函数对象
    sort(v.begin(), v.end(), not2(less<int>()));
    for_each(v.begin(), v.end(), [](int val){cout << val << " "; });

}
//not1 对一元函数对象取反
//not2 对二元函数对象取反

void MyPrint03(int v,int v2)
{
    cout << v + v2<< " ";
}

//3、函数指针适配器   ptr_fun
void test03()
{
    vector <int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    // ptr_fun( )把一个普通的函数指针适配成函数对象
    for_each(v.begin(), v.end(), bind2nd( ptr_fun( MyPrint03 ), 100));
}


//4、成员函数适配器
class Person
{
public:
    Person(string name, int age)
    {
        m_Name = name;
        m_Age = age;
    }

    //打印函数
    void ShowPerson(){
        cout << "成员函数:" << "Name:" << m_Name << " Age:" << m_Age << endl;
    }
    void Plus100()
    {
        m_Age += 100;
    }
public:
    string m_Name;
    int m_Age;
};

void MyPrint04(Person &p)
{
    cout << "姓名:" <<  p.m_Name << " 年龄:" << p.m_Age << endl;

};

void test04()
{
    vector <Person>v;
    Person p1("aaa", 10);
    Person p2("bbb", 20);
    Person p3("ccc", 30);
    Person p4("ddd", 40);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);

    //for_each(v.begin(), v.end(), MyPrint04);
    //利用 mem_fun_ref 将Person内部成员函数适配
    for_each(v.begin(), v.end(), mem_fun_ref(&Person::ShowPerson));
//  for_each(v.begin(), v.end(), mem_fun_ref(&Person::Plus100));
//  for_each(v.begin(), v.end(), mem_fun_ref(&Person::ShowPerson));
}

void test05(){

    vector<Person*> v1;
    //创建数据
    Person p1("aaa", 10);
    Person p2("bbb", 20);
    Person p3("ccc", 30);
    Person p4("ddd", 40);

    v1.push_back(&p1);
    v1.push_back(&p2);
    v1.push_back(&p3);
    v1.push_back(&p4);

    for_each(v1.begin(), v1.end(), mem_fun(&Person::ShowPerson));
}

//如果容器存放的是对象指针,  那么用mem_fun
//如果容器中存放的是对象实体,那么用mem_fun_ref