当前位置: 首页 > news >正文

C++速通Lambda表达式

目录

    • lambda
      • 1、什么是lambda
      • 2、lambda的结构
        • 2.1、捕获列表(capture)
        • 2.2、参数列表(parameters)
        • 2.3、可变规则(mutable)
        • 2.4、 异常说明(noexcept)
        • 2.5、指定返回类型(return_type)
    • 结语

lambda

1、什么是lambda

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

示例demo

#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的结构

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

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

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

值捕获[x]

#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

#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的函数体中改变,如使用引用捕获则可以改变

#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

全部值捕获[=]

#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;
}

全部引用捕获[&]

#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;
}

混合捕获

#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)

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

#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的函数体中可以被改变,但是函数体以外捕获对象的数值是不变得。

#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)

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

#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默认推导出返回值类型,也可以显式指定返回值的类型。

#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;
}

结语

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

http://www.dtcms.com/a/478329.html

相关文章:

  • 微企点做的网站怎么去底下的wordpress首页
  • 高防服务器分为哪几种?香港高防服务器有什么特点?
  • 用 PyTorch 实现 MNIST 手写数字识别:从入门到实践
  • 设计模式篇之 代理模式 Proxy
  • 智联招聘网站建设情况wordpress 注册 密码
  • Mobius Protocol:在“去中心化”逐渐被遗忘的时代,重建秩序的尝试
  • 网站制作公司费用wordpress 宋体、
  • 长宁怎么做网站优化好住房城乡建设门户网站
  • MySQL InnoDB Cluster 高可用集群部署与应用实践(下)
  • commons-rng(伪随机数生成)
  • qemu 串口模拟输入的整个流程
  • 在git commit时利用AI自动生成并填充commit信息
  • 【完整源码+数据集+部署教程】可回收金属垃圾检测系统源码和数据集:改进yolo11-AggregatedAtt
  • HakcMyVM-Crack
  • emmc extcsd寄存器
  • 利用径向柱图探索西班牙语学习数据
  • wordpress建淘宝客网站吗上海网站制作最大的公司
  • 定制网站平台的安全设计房地产公司网站建设
  • 筛法(Sieve Method)简介
  • 【论文阅读】基于指数-高斯混合网络的视频观看时间预测的多粒度分布建模-小红书recsys25
  • 网站开发过程模型做电影网站怎么接广告
  • 手机群控软件在游戏运营中的行为模拟技术实践
  • MySQL----触发器
  • 汕头模板建站平台朝阳市做网站
  • C8051F351-GMR工业用 8051 MCU 开发板C8051F351-GMR嵌入式处理器和控制器,适用于高精度模拟信号处理
  • [嵌入式系统-107]:语音识别的信号处理流程和软硬件职责
  • OkHttp源码解析(一)
  • 拆分PDF.html 办公小工具
  • 网站编辑用什么软件有关于网站建设类似的文章
  • 陶瓷网站制作wordpress导购主题