C++ 11 类模版 std::function

6 篇文章 0 订阅

C++(6)

类模版 std::function

类模版 std::function 是可调用对象的包装器,可以包装除了类成员函数之外的所
有可调用对象。包括,普通函数,函数指针, lambda,仿函数。

通过指定的模板参数,它可以用统一的方式保存,并延迟执行它们。所谓的延迟执
行,就是回调了。

std:function<retType(argType,argType,…)>


#include <iostream>
#include <functional>

using namespace std;

// 定义一个加法函数
int add(int a, int b) {
    return a + b;
}

// 定义一个减法函数
int sub(int a, int b) {
    return a - b;
}
// 定义一个函数指针类型,指向返回值为int,参数为两个int的函数
typedef int (*fun_ptr)(int, int);

// 定义一个类Divide,重载了()运算符,用于实现除法
class Divide{
public:
    int operator()(int a, int b){
        return a / b;
    }
};

int main() {
    // 使用std::function包装add函数
    auto f = function<int(int, int)>(add);
    cout << f(1, 2) << endl; // 输出3

    // 使用函数指针p指向sub函数
    fun_ptr p = sub;
    // 将函数指针p包装进std::function
    f = function<int(int, int)>(p);
    cout << f(1, 2) << endl; // 输出-1

    // 定义一个lambda表达式mul,用于实现乘法
    auto mul = [](int a, int b) {
        return a * b;
    };
    // 将lambda表达式mul包装进std::function
    f = function<int(int, int)>(mul);
    cout << f(2, 3) << endl; // 输出6

    // 创建Divide类的实例div
    Divide div;
    // 将div对象包装进std::function
    f = function<int(int, int)>(div);
    cout << f(10, 3) << endl; // 输出3

    return 0;
}

map中使用

#include <iostream>
#include <map>
#include <functional>
using namespace std;


// 定义一个加法函数
int add(int a, int b) {
    return a + b;
}

// 定义一个减法函数
int sub(int a, int b) {
    return a - b;
}
// 定义一个函数指针类型,指向返回值为int,参数为两个int的函数
typedef int (*fun_ptr)(int, int);

// 定义一个类Divide,重载了()运算符,用于实现除法
class Divide{
public:
    int operator()(int a, int b){
        return a / b;
    }
};


int main(){
    // 定义一个lambda表达式mul,用于实现乘法
    auto mul = [](int a, int b) {
        return a * b;
    };
    map< string , function<int(int,int)> > mp={
            {"+",add},
            {"-",sub},
            {"*",mul},
            {"/",Divide()}
    };
    // 调用map中保存的函数
    cout<<mp["+"](1,2)<<endl;//3
    cout<<mp["-"](1,2)<<endl;//-1
    cout<<mp["*"](2,3)<<endl;//6
    cout<<mp["/"](10,3)<<endl;//3

    return 0;
}

fucntion 作参数类型实现回调

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

// 选择排序函数,接受一个整数数组指针、数组长度和一个比较函数
void selsctSort(int *p, int n, function<int(int, int)> com) {
    
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            // 使用传入的比较函数com来比较当前元素p[i]和p[j]
            if (com(p[i], p[j])) {
                p[i] ^= p[j];
                p[j] ^= p[i];
                p[i] ^= p[j];
            }
        }
    }
}

int main() {

    int arr[] = {5, 3, 8, 6, 2, 7, 4, 1};
    int n = sizeof(arr) / sizeof(*arr);
    // 调用选择排序函数,传入数组、数组长度和一个lambda表达式作为比较函数
    // 该lambda表达式定义了比较规则
    selsctSort(arr, n, [](int a, int b) { return a > b; });

    // 使用for_each算法遍历排序后的数组,并输出每个元素
    // 传入的lambda表达式用于定义对每个元素的操作,这里只是简单地输出元素
    for_each(arr, arr + n, [](int &a) { cout << a << " "; });
    return 0;
}


fucntion 作类成员实现回调

#include <iostream>
#include <list>
#include <functional>
using namespace std;

// 定义三个仿函数类,每个类重载了()操作符,用于执行特定的操作
class FunctorA
{
public:
    void operator()(){
        cout << "class Functor A" << endl;
    }
};

class FunctorB
{
public:
    void operator()(){
        cout << "class Functor B" << endl;
    }
};

class FunctorC
{
public:
    void operator()(){
        cout << "class Functor C" << endl;
    }
};


class Object
{
public:

    Object(FunctorA a, FunctorB b, FunctorC c)
    {

        _list.push_back(a);
        _list.push_back(b);
        _list.push_back(c);
    }


    void notify()
    {
        for(auto &func : _list){
            func();
        }
    }

private:
    // 使用std::function模板来存储可调用对象
    list< function<void(void)> > _list;
};


int main()
{

    FunctorA a;
    FunctorB b;
    FunctorC c;
    Object obj(a, b, c);
    obj.notify();
    return 0;
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarkTop1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值