C++复习笔记--STL的函数对象(仿函数)的使用

1--函数对象

1-1--基本概念和使用方法

        基本概念:重载函数调用操作符的类,其对象常称为函数对象;函数对象使用重载的()时,行为类似于函数调用,因此也称为仿函数;

        本质:函数对象(仿函数)本质上是一个类,不是一个函数;

        使用方法:

        函数对象在使用时,可以像普通函数那样调用,可以有参数,也可以有返回值;

        不同于普通函数,函数对象可以拥有自己的状态

        函数对象可以作为参数进行传递;

1-2--代码实例:

① 普通函数调用

#include "iostream"

class MyAdd{
public:
    int operator()(int v1, int v2){
        return v1 + v2;
    }
};

int main(int argc, char* argv[]){
    MyAdd myadd;
    std::cout << myadd(10, 10) << std::endl;
    return 0;
}

②  拥有专属状态:

#include "iostream"
#include "string"
class MyPrint{
public:
    void operator()(std::string test){
        std::cout << test << std::endl;
        this->count ++;
    }
    int count; // 通过count记录被调用的次数
};

int main(int argc, char* argv[]){
    MyPrint myprint;
    myprint("Hello!!");
    myprint("Hello!!");
    myprint("Hello!!");
    myprint("Hello!!");
    std::cout << "Call nums: " << myprint.count << std::endl;
    return 0;
}

        上述代码中,通过 count 变量用于记录函数对象被调用的次数,即拥有了属于自己的状态;

③ 作为参数传递:

#include "iostream"
#include "string"
class MyPrint{
public:
    void operator()(std::string test){
        std::cout << test << std::endl;
        this->count ++;
    }
    int count; // 通过count记录被调用的次数
};

void doPrint(MyPrint &mp, std::string test){
    mp(test);
}

int main(int argc, char* argv[]){
    MyPrint myprint;
    std::string test = "Hello!";
    doPrint(myprint, test); // 作为参数进行传递
    return 0;
}

2--谓词

基本概念:

返回 bool 类型的仿函数称为谓词;

如果 operator() 接收一个参数,则称为一元谓词;

如果operator() 接收两个参数,则称为二元谓词;

代码实例:

        ① 一元谓词代码实例:

#include "iostream"
#include "vector"
#include "algorithm"

class GreaterFive{
public:
    bool operator()(int val) {
        return val > 5;
    }
};

int main(int argc, char* argv[]){
    std::vector<int> v1;
    for(int i = 1; i <= 10; i++){
        v1.push_back(i);
    }

    std::vector<int>::iterator it = std::find_if(v1.begin(), v1.end(), GreaterFive());
    // GreaterFive() 表示匿名函数对象
    if(it != v1.end()){
        std::cout << "Sucessfully find: " << *it << std::endl;
    }
    else{
        std::cout << "Can not find the elem!" << std::endl;
    }

    return 0;
}

        ② 二元谓词代码实例:

#include "iostream"
#include "vector"
#include "algorithm"

class GreaterFive{
public:
    bool operator()(int val1, int val2) {
        return val1 > val2;
    }
};

int main(int argc, char* argv[]){
    std::vector<int> v1;
    v1.push_back(10);
    v1.push_back(30);
    v1.push_back(20);
    v1.push_back(40);

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

    return 0;
}

3--内建函数对象

        STL 提供了部分内建函数对象,其主要分为三类:算术仿函数、关系仿函数和逻辑仿函数;

3-1--算术仿函数

        算术仿函数可分为一元仿函数和二元仿函数,其原型如下:

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

        代码实例:

#include "iostream"
#include "functional"

int main(int argc, char* argv[]){

    std::negate<int> n; // 取反仿函数
    std::cout << n(50) << std::endl;

    std::plus<int> add;
    std::cout << add(10, 20) << std::endl;
    
    return 0;
}

3-2--关系仿函数

        部分关系仿函数如下:

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

        代码实例:

#include "iostream"
#include "functional"
#include "vector"
#include "algorithm"

int main(int argc, char* argv[]){

    std::vector<int> v1;
    v1.push_back(10);
    v1.push_back(30);
    v1.push_back(20);
    v1.push_back(40);
    std::sort(v1.begin(), v1.end(), std::greater<int>());
    for(std::vector<int>::iterator it = v1.begin(); it != v1.end(); it++){
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    return 0;
}

3-3--逻辑仿函数

        常用逻辑仿函数的函数原型如下:

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

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

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

        实例代码:

#include "iostream"
#include "functional"
#include "vector"
#include "algorithm"

int main(int argc, char* argv[]){

    std::vector<bool> v1;
    v1.push_back(true);
    v1.push_back(false);
    v1.push_back(true);
    v1.push_back(false);
    for(std::vector<bool>::iterator it = v1.begin(); it != v1.end(); it++){
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    std::vector<bool> v2;
    v2.resize(v1.size());
    std::transform(v1.begin(), v1.end(), v2.begin(), std::logical_not());
    for(std::vector<bool>::iterator it = v2.begin(); it != v2.end(); it++){
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43863869/article/details/129757138