【C++函数对象】

366 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

本文已参与「掘力星计划」,赢取创作大礼包,挑战创作激励金。

函数对象

【概念】:

  • 重载函数调用操作符的类,其对象常称为函数对象
  • 函数对象使用重载的()时,行为类似函数调用,也叫仿函数

【本质】:

函数对象(仿函数)是一个不是函数

函数对象的使用

【特点】:

  • 函数对象在使用时,可以像普通函数一样调用,可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递 【demo】:
#include <iostream>
#include <string>

using namespace std;
// 1、函数对象在使用时,可以像普通函数一样调用,可以有参数,可以有返回值
class MyAdd
{
public:
    int operator()(int v1, int v2)
    {
        return v1 + v2;
    }
};

// 2、函数对象可以有自己的状态
class MyPrint
{
public:
    MyPrint()
    {
        this->count = 0;
    }
    void operator()(string test)
    {
        cout << test << endl;
        this->count++; // 统计输出的次数
    }

public:
    int count;
};

void test01()
{
    MyAdd myAdd;
    cout << myAdd(1, 2) << endl;
}

void test02()
{
    MyPrint myPrint;
    myPrint("hello c++");
    myPrint("hello c++");
    myPrint("hello c++");
    cout << "myPrint被调用的次数为:" << myPrint.count << endl;
}

// 3、函数对象可以作为参数传递
void doPrint(MyPrint &mp, string test)
{
    mp(test);
}
void test03()
{
    MyPrint myPrint;
    doPrint(myPrint, "myPrint作为参数传递");
}
int main()
{
    test01();
    test02();
    test03();
    return 0;
}

谓词

【概念】:

  • 返回bool类型的仿函数为谓词
  • operator()接受一个参数,那么叫做一元谓词
  • operator()接受两个参数,那么叫做二元谓词 【demo】:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

// 一元谓词
struct GreaterFive
{

    bool operator()(int val)
    {
        return val > 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(), GreaterFive());
    if (it == v.end())
    {

        cout << "未找到" << endl;
    }
    else
    {
        cout << "find it : " << *it << endl;
    }
}

// 二元谓词
class MyCompare
{
public:
    bool operator()(int v1, int v2)
    {
        return v1 > v2;
    }
};
void printVector(vector<int> &v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void test02()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    //默认从小到大排序
    cout << "默认从小到大排序" << endl;
    sort(v.begin(), v.end());
    printVector(v);
    // 使用函数对象改变算法策略,排序从大到小
    cout << "使用函数对象改变排序策略,从大到小" << endl;
    sort(v.begin(), v.end(), MyCompare());
    printVector(v);
}

int main()
{
    test01();
    test02();
    return 0;
}

内建函数对象

【概念】:

  • STL内建了一些函数对象

【分类】:

  • 算数仿函数

  • 关系仿函数

  • 逻辑仿函数 【用法】:

  • 这些仿函数所产生的对象,用法和一般函数完全相同

  • 使用内建函数对象,需要引入头文件 #include<functional>

算数仿函数

【功能描述】:

  • 实现四则运算

  • 其中negate 是一元运算,其他都是二元运算

【函数原型】:

- 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>; // 取反仿函数

【demo】:

#include <iostream>
#include <functional>

using namespace std;

// negate
void test01()
{
    negate<int> n;
    cout << "negate<int> 50 : " << n(50) << endl;
}

// plus
void test02()
{
    plus<int> p;
    cout << "plus<int> (10,20) : " << p(10, 20) << endl;
}

// minus
void test03()
{
    minus<int> m;
    cout << "minus<int> (20,10) : " << m(20, 10) << endl;
}

// multiplies
void test04()
{
    multiplies<int> mul;
    cout << "multiplies<int> (3,5) : " << mul(3, 5) << endl;
}

// divides
void test05()
{
    divides<int> d;
    cout << "divides<int> (6,2) : " << d(6, 2) << endl;
}

// modulus
void test06()
{
    modulus<int> mod;
    cout << "modulus<int> (9,3) : " << mod(9, 3) << endl;
}

int main()
{
    test01();
    test02();
    test03();
    test04();
    test05();
    test06();

    return 0;
}

关系仿函数

【功能描述】:

  • 实现关系对比

【函数原型】:

- 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>; //小于等于

【demo】:

#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>

using namespace std;
class MyCompare
{
public:
    bool operator()(int num1, int num2)
    {
        return num1 > num2;
    }
};

void printVector(vector<int> &v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
void test01()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    cout << "默认从小到大排序" << endl;
    sort(v.begin(), v.end());
    printVector(v);

    cout << "自己实现仿函数,从大到小排序" << endl;
    sort(v.begin(), v.end(), MyCompare());
    printVector(v);
    cout << "使用内建仿函数,从大到小排序" << endl;
    sort(v.begin(), v.end(), greater<int>());
    printVector(v);
}

int main()
{
    test01();
    return 0;
}

逻辑仿函数

【功能描述】:

  • 实现逻辑运算

【函数原型】:

- template<class T> bool logical_and<T>; // 逻辑与

- template<class T> bool logical_or<T>; // 逻辑或

- template<class T> bool logical_not<T>; // 逻辑非

【demo】:

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

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

void test01()
{
    vector<bool> v;
    v.push_back(true);
    v.push_back(false);
    v.push_back(true);
    v.push_back(false);
    v.push_back(true);
    cout << "v1" << endl;
    printVector(v);

    // 逻辑非,将容器v搬运到v2中,并执行逻辑非运算
    vector<bool> v2;

    v2.resize(v.size());
    transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
    cout << "v2" << endl;
    printVector(v2);
}

int main()
{
    test01();
    return 0;
}

小结:仿函数实际应用比较少