Lambda 函数与表达式

时间:2019-05-18 13:26:51   收藏:0   阅读:246

Lambda函数的语法定义如下:

[capture](parameters) mutable ->return-type{statement}

  其中:

  在 lambda 函数的定义式中,参数列表和返回类型都是可选部分,而捕捉列表和函数体都可能为空,C++ 中最简单的 lambda 函数只需要声明为:[]{};

对于[]:

举个栗子:

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int ex = 10;   //这里设置一个值让下面的lamdba表达式去捕获
 5     auto temp = [ex](int a) -> int {return a;}; //基本完整的写法
 6     auto temp_other = [ex](int b){return b;};  //没有设置返回类型,让编译器自动推断
 7     auto temp_otherone = [ex]{return ex;};  //再把参数去掉,这就是最简单的表达式
 8     printf("temp=%d\n",temp);
 9     printf("temp_other=%d\n",temp_other);
10     printf("temp_otherone=%d\n",temp_otherone);
11     return 0 ;
12 }

 

原文:https://www.cnblogs.com/xiaodangxiansheng/p/10885296.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!