栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ 11 中 function,lambda,bind

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ 11 中 function,lambda,bind

c++ 11 中 function,lambda,bind之间关系
  • 什么是 function、 lambda,bind?用来做什么?怎么用?
  • function,lambda,bind 之间有什么关系?
  • 具体怎么实现?

function 实例
//function 是一个抽象了函数参数以及函数返回值的类模板
#include 
#include 
#include 

using namespace std;

void hello(int count) {
    cout << "hello gq:" << count << endl;
}

class StaticFunc {
public: 
    static void hello(int count) {
        cout << "StaticFunc::hello gq:" << count << endl; 
    }
};

class Hello {
public: 
    void operator() (int count) {
        i += count;
        cout << "Hello::hello gq:" << i << endl;
    }
    void operator() (int a, int b) {
        cout << "Hello::hello gq:a+b=" << a + b << endl;
    }

    int i = 0;
};

class CHello {
public:
    void hello(int count) {
        cout << "CHello::hello gq:" << count << endl; 
    }
};

class LambdaHello {  //注意:类比lambda表达式展开
public:
    LambdaHello(int _i) : i(_i) {}
    void operator()(int count) {
        i ++;
        cout << "lambda hello gq:" << count << " i=" << i << endl;
    }
private:
    int i;
};

class BindHello {  //相当于auto f_hello9 = bind(&hello, 9); 的展开
public:
    BindHello(function _fn, int _count) : fn(_fn), count(_count) {};
    void operator()() {
        fn(count);
    }
private:
    function fn;
    int count;
};

class BindCHello {  //相当于f_hello11 = bind(&CHello::hello, &c, placeholders::_1); 的展开
public:
    typedef void (CHello::*Fn)(int);
    BindCHello(Fn _fn, CHello *_c) : fn(_fn), c(_c) {}
    void operator() (int count) {
        (c->*fn)(count);
    }
private:
    Fn fn;
    CHello* c;
};

int main(){
    function f_hello1 = hello;  //保存普通函数;抽象函数参数和返回值
    f_hello1(1);  //hello gq:1

    function f_hello2 = &hello; //指向具体函数的地址或者具体函数
    f_hello2(1);  //hello gq:1

    function f_hello3 = &StaticFunc::hello;  //保存类的静态成员函数
    f_hello3(2);  //StaticFunc::hello gq:2

    cout << "======保存仿函数:有状态,通过成员变量进行存储状态:======" << endl;
    function f_hello4 = Hello();  //调用默认构造生成仿函数对象
    f_hello4(4);  //Hello::hello gq:4
    f_hello4(4);  //Hello::hello gq:8

    function f_hello5 = Hello();
    f_hello5(5, 6);  //Hello::hello gq:a+b=11

    cout << "======保存类成员函数======" << endl;
    function f_hello6 = &CHello::hello;
    CHello c;
    f_hello6(&c, 7);  //CHello::hello gq:7

    cout << "======保存lambda表达式======" << endl;
    int i = 0;
    auto f_hello7 = [i](int count) mutable -> void {  //如若修改外部变量则采用[&i]
        i ++;
        cout << "lambda hello gq:" << count << " i=" << i << endl;
    };
    f_hello7(8);  //lambda hello gq:8 i=1
    f_hello7(8);  //lambda hello gq:8 i=2
    cout << "外部变量 i=" << i << "不修改外部变量" << endl; 

    auto f_hello8 = LambdaHello(i);  //这里 i 参数相当于值传递
    f_hello8(9);  //lambda hello gq:9 i=1

    cout << "======保存bind返回的函数对象======" << endl;
    auto f_hello9 = bind(&hello, 9);  //绑定普通函数 对应 
    f_hello9();  //hello gq:9

    auto f_hello10 = bind(&CHello::hello, &c, 10);  //绑定类成员函数
    f_hello10();  //CHello::hello gq:10

    auto f_hello11 = bind(&CHello::hello, &c, placeholders::_1);  //提供占位符
    f_hello11(11);  //CHello::hello gq:11

    auto f_hello12 = BindHello(&hello, 9);  //等价于f_hello9()
    f_hello12();  //hello gq:9

    auto f_hello13 = BindCHello(&CHello::hello, &c);  //等价于f_hello11()
    f_hello13(11);  //CHello::hello gq:11

    return 0;
}
代码执行结果:
hello gq:1
hello gq:1
StaticFunc::hello gq:2
======保存仿函数:有状态,通过成员变量进行存储状态:======
Hello::hello gq:4
Hello::hello gq:8
Hello::hello gq:a+b=11
======保存类成员函数======
CHello::hello gq:7
======保存lambda表达式======
lambda hello gq:8 i=1
lambda hello gq:8 i=2
外部变量 i=0不修改外部变量
lambda hello gq:9 i=1
======保存bind返回的函数对象======
hello gq:9
CHello::hello gq:10
CHello::hello gq:11
hello gq:9
CHello::hello gq:11

注意:

  • lambda 表达式展开类比。
  • bind 绑定普通成员函数展开。
  • bind 绑定类成员函数展开。

充电站
推荐一个零声学院免费公开课程,个人觉得老师讲得不错,分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1017918.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号