c++ 11 中 function,lambda,bind

简介: c++ 11 中 function,lambda,bind

c++ 11 中 function,lambda,bind之间关系

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

function 实例

//function 是一个抽象了函数参数以及函数返回值的类模板
#include <functional>
#include <iostream>
#include <algorithm>
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<void(int)> _fn, int _count) : fn(_fn), count(_count) {};
    void operator()() {
        fn(count);
    }
private:
    function<void(int)> 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<void(int)> f_hello1 = hello;  //保存普通函数;<void(int)>抽象函数参数和返回值
    f_hello1(1);  //hello gq:1
    function<void(int)> f_hello2 = &hello; //指向具体函数的地址或者具体函数
    f_hello2(1);  //hello gq:1
    function<void(int)> f_hello3 = &StaticFunc::hello;  //保存类的静态成员函数
    f_hello3(2);  //StaticFunc::hello gq:2
    cout << "======保存仿函数:有状态,通过成员变量进行存储状态:======" << endl;
    function<void(int)> f_hello4 = Hello();  //调用默认构造生成仿函数对象
    f_hello4(4);  //Hello::hello gq:4
    f_hello4(4);  //Hello::hello gq:8
    function<void(int,int)> f_hello5 = Hello();
    f_hello5(5, 6);  //Hello::hello gq:a+b=11
    cout << "======保存类成员函数======" << endl;
    function<void(CHello*, int)> 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等技术内容,立即学习

相关文章
|
3月前
|
程序员 编译器 C++
【实战指南】C++ lambda表达式使用总结
Lambda表达式是C++11引入的特性,简洁灵活,可作为匿名函数使用,支持捕获变量,提升代码可读性与开发效率。本文详解其基本用法与捕获机制。
142 44
|
6月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
206 12
|
11月前
|
算法 编译器 C++
【C++11】lambda表达式
C++11 引入了 Lambda 表达式,这是一种定义匿名函数的方式,极大提升了代码的简洁性和可维护性。本文详细介绍了 Lambda 表达式的语法、捕获机制及应用场景,包括在标准算法、排序和事件回调中的使用,以及高级特性如捕获 `this` 指针和可变 Lambda 表达式。通过这些内容,读者可以全面掌握 Lambda 表达式,提升 C++ 编程技能。
495 3
|
存储 算法 程序员
C++ 11新特性之function
C++ 11新特性之function
322 9
|
存储 编译器 调度
C++ 11新特性之bind
C++ 11新特性之bind
116 1
|
C++ 容器
函数对象包装器function和bind机制
函数对象包装器function和bind机制
89 0
|
C++ 算法
c++中 lambda 表达式 解析
c++中 lambda 表达式 解析
149 0
c++中 lambda 表达式 解析
|
算法 编译器 程序员
C++ 11新特性之Lambda表达式
C++ 11新特性之Lambda表达式
88 0
|
存储 C++ 运维
开发与运维函数问题之使用C++标准库中的std::function来简化回调函数的使用如何解决
开发与运维函数问题之使用C++标准库中的std::function来简化回调函数的使用如何解决
149 6
|
安全 编译器 C++
C++一分钟之-泛型Lambda表达式
【7月更文挑战第16天】C++14引入泛型lambda,允许lambda接受任意类型参数,如`[](auto a, auto b) { return a + b; }`。但这也带来类型推导失败、隐式转换和模板参数推导等问题。要避免这些问题,可以明确类型约束、限制隐式转换或显式指定模板参数。示例中,`safeAdd` lambda使用`static_assert`确保只对算术类型执行,展示了一种安全使用泛型lambda的方法。
189 1

热门文章

最新文章

下一篇
oss教程