C++速通Lambda表达式

目录

lambda

1、什么是lambda

lambda 表达式 是 C++11 引入的一种语法,让你可以在代码中直接定义一个"匿名函数",不需要写函数名。

示例demo

cpp 复制代码
#include <iostream>
#include <cstdio>
int main()
{
    auto add = [](int a, int b) -> int
    {
        return a + b;
    };

    printf("add : %d\n", add(3, 4));
    return 0;
}

2、lambda的结构

cpp 复制代码
[capture](parameters) mutable noexcept -> return_type {
// 函数体
}

capture     :  捕获列表
parameters  : 参数列表
mutable     : 可变规则
noexcept    : 异常说明
return_type : 指定返回类型
{}          :  函数体
2.1、捕获列表(capture)

捕获列表一共有下述五种类型

捕获方式 写法 含义
值捕获 [x] 拷贝外部变量 x 的值
引用捕获 [&x] 按引用使用外部变量 x
全部值捕获 [=] 拷贝父作用域所有外部变量
全部引用捕获 [&] 引用父作用域所有外部变量
混合捕获 [=, &y] / [&, x] 大部分按某种方式捕获,个别特殊指定

值捕获x

cpp 复制代码
#include <iostream>
#include <cstdio>
int main()
{
    int a = 1;
    auto GetPrama = [a](){
        printf("a : %d\n", a);
    };

    GetPrama();
    return 0;
}

1、此方式不能在lambda表达式内部改变捕获变量的数值, 也可以理解为传入的参数a是const类型的变量,因此 无法在函数体内修改。

2、此方式还可以捕获this

cpp 复制代码
#include <iostream>
#include <cstdio>

class TestLambda
{
public:
    TestLambda() = default;
    ~TestLambda() = default;
    TestLambda(const TestLambda &other) = delete;
    TestLambda& operator=(const TestLambda &other) = delete;
    TestLambda(TestLambda &&other) = delete;
    TestLambda& operator=(TestLambda &&other) = delete;

public:
    void LambadFunc()
    {
        auto func = [this](){
            this->Print();
        };

        func();
    }

private:
    void Print()
    {
        printf("i am : %s\n", __FUNCTION__);
    }
};

int main()
{
    TestLambda t;
    t.LambadFunc();
    return 0;
}

3、如果**\[\]**中什么也不写,则不捕获任何东西。

引用捕获\&x

在值捕获 的例子中我们的a无法在lambda的函数体中改变,如使用引用捕获则可以改变

cpp 复制代码
#include <iostream>
#include <cstdio>
int main()
{
    int a = 1;
    auto GetPrama = [&a](){
        a = 6;
        return a;
    };

    printf("a : %d\n", GetPrama());
    return 0;
}

ps:此方式也适用于引用捕获this

全部值捕获=

cpp 复制代码
#include <iostream>
#include <cstdio>
int main()
{
    int a = 1, b = 2, c = 3;
    auto GetPrama = [=]()
    {
        printf("a : %d, b : %d, c : %d\n", a, b, c);
    };
    GetPrama();
    return 0;
}

全部引用捕获\&

cpp 复制代码
#include <iostream>
#include <cstdio>
#include <functional>
int main()
{
    int a = 1, b = 2, c = 3;
    std::function<void(void)> GetPrama = [&]()
    {
        a = 6;
        b = 6;
        c = 6;
    };
    GetPrama();
    printf("a : %d, b : %d, c : %d\n", a, b, c);
    return 0;
}

混合捕获

cpp 复制代码
#include <iostream>
#include <cstdio>
#include <functional>
int main()
{
    int a = 1, b = 2, c = 3;
    std::function<void(void)> GetPrama1 = [&, b]() // 全部引用捕获但是b除外, b是值捕获
    {
        a = 6;
        // b = 6; 唯独b是值捕获,因此无法在lambda的函数体中修改。
        c = 6;
    };
    GetPrama1();
    printf("a : %d, b : %d, c : %d\n", a, b, c);

    // std::function<void(void)> GetPrama2 = [b, &]() //<---b说明是值捕获b,然后&是全部引用捕获。
    // {                                              // 混合捕获只支持这两种模式 [=, &y, &z ....] [&, x, y, z ....]
    //     a = 6;
    //     b = 6; 
    //     c = 6;
    // };

    std::function<void(void)> GetPrama3 = [=, &b, &c]() // 全部引用捕获但是b除外, b是值捕获
    {
        // a = 6; 唯独a是值捕获,因此无法在lambda的函数体中修改。
        b = 8; 
        c = 8;
    };
    GetPrama3();
    printf("a : %d, b : %d, c : %d\n", a, b, c);
    return 0;
}
2.2、参数列表(parameters)

参数列表很好理解,就当函数参数一样定义就行

cpp 复制代码
#include <iostream>
#include <cstdio>
#include <functional>
int main()
{
    int a = 1, b = 2, c = 3;
    auto GetPrama1 = [a, b](int &x, int &y)
    {
        x = a;
        y = b;
    };
    int x, y;
    GetPrama1(x, y);
    printf("x : %d, y : %d\n", x, y);
    return 0;
}
2.3、可变规则(mutable)

上面我们说了值捕获无法修改捕获对象的数值,但是使用mutable可以强制捕获,让其在lambda的函数体中可以被改变,但是函数体以外捕获对象的数值是不变得。

cpp 复制代码
#include <iostream>
#include <cstdio>
#include <functional>
int main()
{
    int a = 1, b = 2, c = 3;
    auto GetPrama1 = [a]() mutable
    {
        a = 6;
        printf("GetPrama1 a : %d\n", a);
    };
    GetPrama1();
    printf("main a : %d\n", a); //原本a的数不会被改变。
    return 0;
}
2.4、 异常说明(noexcept)

这个一般不咋用到,简单点说如果你认为你的函数体内不会抛出异常,你就可以带上这个参数。

cpp 复制代码
#include <iostream>
#include <cstdio>
#include <functional>
int main()
{
    int a = 1;
    auto GetPrama1 = [a]() mutable noexcept
    {
        a = 6;
    };
    GetPrama1();
    printf("a : %d\n", a);
    return 0;
}
2.5、指定返回类型(return_type)

lambda默认推导出返回值类型,也可以显式指定返回值的类型。

cpp 复制代码
#include <iostream>
#include <cstdio>
#include <functional>
int main()
{
    auto GetPrama = []() mutable noexcept -> double
    {
        int a = 6, b = 2;
        return static_cast<double>(a/b);
    };
    printf("val : %f\n", GetPrama());
    return 0;
}

结语

感谢您的阅读,如有问题可以私信或评论区交流。

^ _ ^

相关推荐
eggwyw1 小时前
linux下的C\C++多进程多线程编程实例详解
开发语言·c++
布莱克6051 小时前
Swift 枚举详解:从基础语法到关联值与模式匹配
开发语言·ios·swift·枚举
布莱克6052 小时前
Swift 闭包详解:从语法到实战的完整指南
服务器·开发语言·ios·ssh·swift·闭包
青少儿编程课堂2 小时前
第十八届蓝桥杯青少组图形化真题 + 全国青少年信息素养大赛 + 电子学会图形化考级:校园泡泡堂大挑战解析
c++·python·算法·bfs·信息学竞赛
小七在进步3 小时前
string的实现(3)
开发语言·c++·笔记·visual studio
青少儿编程课堂3 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:Dinic最大流解析
c++·python·算法·bfs·信息学竞赛
专注仿真4 小时前
基于 AFSIM 的智能体训练:多智能体强化学习篇 —— QTRAN 算法实战
开发语言·python·算法·php·强化学习·afsim智能体·仿真训练
草莓熊Lotso4 小时前
【C++ 基于微服务的即时通讯系统】用户服务之Redis数据层落地:会话、登录状态与验证码全封装
数据库·c++·redis·微服务·语音识别
西西弗Sisyphus4 小时前
C++ 里 两个独立的所有权域之间没有建立同步的问题
c++
Funlang5 小时前
RISC-V 上的 Fun:第四套机器码
开发语言·risc-v·龙芯