• 匿名函数lambda


    一、匿名函数的基本语法

    // mutable 是可选的
    [捕获列表](参数列表) mutable 异常属性 -> 返回类型 {
    // 函数体
    }
    
    • 1
    • 2
    • 3
    • 4

    语法规则:lambda表达式可以看成是一般函数的函数名被略去,返回值使用了一个->的形式表示。唯一与普通函数不同的是增加了“捕获列表”。

    //[捕获列表](参数列表)->返回类型{函数体}
    int main()
    {
      auto Add = [](int a, int b)->int {
        return a + b;
     };
      std::cout << Add(1, 2) << std::endl;     //输出3
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    一般情况下,编译器可以自动推断出lambda表达式的返回类型,所以我们可以不指定返回类型,即:

    //[捕获列表](参数列表){函数体}
    int main()
    {
      auto Add = [](int a, int b) {
        return a + b;
     };
      std::cout << Add(1, 2) << std::endl;     //输出3
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    但是如果函数体内有多个return语句时,编译器无法自动推断出返回类型,此时必须指定返回类型。

    二、捕获列表

    有时候,需要在匿名函数内使用外部变量,所以用捕获列表来传递参数。根据传递参数的行为,捕获列表可分为以下几种

    2.1 值捕获

    与参数传值类似,值捕获的前提是变量可以拷贝,不同之处则在于,被捕获的变量在 lambda表达式被创建时拷贝,而非调用时才拷贝:

    void test3()
    {
      int c = 12;
      int d = 30;
      auto Add = [c, d](int a, int b)->int {
        cout << "d = " << d  << endl;
        return c;
     };
      d = 20;
      std::cout << Add(1, 2) << std::endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    d = 30
    12
    
    • 1
    • 2

    2.2 引用捕获

    与引用传参类似,引用捕获保存的是引用,值会发生变化。

      int c = 12;
      int d = 30;
      auto Add = [&c, &d](int a, int b)->int {
        c = a; // 编译对的
        cout << "d = " << d  << endl;
        return c;
     };
      d = 20;
      std::cout << Add(1, 2) << std::endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    d = 20
    1
    
    • 1
    • 2

    2.3 隐式捕获

    手动书写捕获列表有时候是非常复杂的,这种机械性的工作可以交给编译器来处理,这时候可以在捕获列表中写一个 & = 向编译器声明采用引用捕获或者值捕获

    传入&

    int main(int argc, char * argv[])
    {
      int c = 12;
      int d = 30;
      auto Add = [&](int a, int b)->int {
        c = a; 
        cout << "d = " << d  << endl;
        return c;
     };
      d = 20;
      std::cout << Add(1, 2) << std::endl;
      std::cout << "c = " << c<< std::endl;
      std::cout << "d = " << d<< std::endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    d = 20
    1
    c = 1
    d = 20
    
    • 1
    • 2
    • 3
    • 4

    传入=

    int main(int argc, char * argv[])
    {
      int c = 12;
      int d = 30;
      auto Add = [=](int a, int b)->int {
        // c = a; 此时这一报错,被捕获的变量在 lambda表达式被创建时拷贝
        cout << "d = " << d  << endl;
        return c;
     };
      d = 20;
      std::cout << Add(1, 2) << std::endl;
      std::cout << "c = " << c<< std::endl;
      std::cout << "d = " << d<< std::endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    d = 30
    12
    c = 12
    d = 20
    
    • 1
    • 2
    • 3
    • 4

    2.4 空捕获列表

    捕获列表’[]'中为空,表示Lambda不能使用所在函数中的变量。

      int c = 12;
      int d = 30;
    
      // [] 空值,不能使用外面的变量
      auto Add = [](int a, int b)->int {
        cout << "d = " << d  << endl; // 编译报错
        return c;// 编译报错
     };
      d = 20;
      std::cout << Add(1, 2) << std::endl;
      std::cout << "c:" << c<< std::endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.5 表达式捕获

    上面提到的值捕获、引用捕获都是已经在外层作用域声明的变量,因此这些捕获方式捕获的均为左值,而不能捕获右值。

    C++14之后支持捕获右值,允许捕获的成员用任意的表达式进行初始化,被声明的捕获变量类型会根据表达式进行判断,判断方式与使用 auto 本质上是相同的:

    int main(int argc, char * argv[])
    {
      auto important = std::make_unique<int>(1);
      auto add = [v1 = 1, v2 = std::move(important)](int x, int y) -> int {
        return x + y + v1 + (*v2);
     };
      std::cout << add(3,4) << std::endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    9
    
    • 1

    三、泛型 Lambda

    在C++14之前,lambda表示的形参只能指定具体的类型,没法泛型化。从 C++14 开始, Lambda 函数的形式参数可以使用 auto关键字来产生意义上的泛型:

    int main(int argc, char * argv[])
    {
      auto add = [](auto x, auto y) {
        return x+y;
     };
      std::cout <<  add(1, 2) << std::endl;
      std::cout <<  add(1.1, 1.2) << std::endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    四、可变lambda

    1)采用值捕获的方式,lambda不能修改其值,如果想要修改,使用mutable修饰

    void test() {
    	int v = 5;
    	// 值捕获方式,使用mutable修饰,可以改变捕获的变量值
    	auto ff = [v]() mutable {return ++v;};
    	v = 0;
    	auto j = ff(); // j为6
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2)采用引用捕获的方式,lambda可以直接修改其值

    void test13() {
    	int v = 5;
    	// 采用引用捕获方式,可以直接修改变量值
    	auto ff = [&v] {return ++v;};
    	v = 0;
    	auto j = ff(); // v引用已修改,j为1
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    机器视觉_工业相机及相关配件选型
    nodejs+vue城市轨道交通线路查询系统-计算机毕业设计
    【LLM大模型】大模型也内卷,Vicuna训练及推理指南,效果碾压斯坦福羊驼
    烟感监控小技巧,这才是最高级的方法!
    尚硅谷设计模式(十九)中介者模式
    [数据结构]~栈和队列(0-1)
    第十五届蓝桥杯省赛第二场C/C++B组D题【前缀总分】题解(AC)
    数据结构与算法学习(day1)——简化版桶排序
    CMT2380F32模块开发5-CLK例程
    算法--贪心算法的应用
  • 原文地址:https://blog.csdn.net/Ricardo2/article/details/132691326