0. lambda emerged since c++11, lambda expression/function is an unnamed function object capable of capturing variables in scope.

A lambda function is a function that you can write inline in your source code (usually to pass in to another function, similar to the idea of a function pointer).

1. syntax of a lambda expression

[ captures ] <tparams>(optional)(c++20) ( params ) specifiers exception attr -> ret requires(optional)(c++20){ body }

[ captures ] ( params ) -> ret { body }

[ captures ] ( params ) { body }

[ captures ] { body }

2. Examples:

[](){};   // barebone lambda, [] is the capture list, ()is the argument list, {} is the function body

[](){}(); // immediately execute a lambda or call the lambda function

auto pr = [](int& n){n = n+1; std::cout << " " << n;};  // define a lambda function, and assign it to the pr

#include <algorithm>  // std::for_each()

std::for_each(v.begin(), v.end(), pr); // caller of the lambda expression

std::for_each(v.begin(), v.end(), [](int &n){ n++; std::cout << " " << n; }); // the same lambda function caller

3. About capture list

[] Capture nothing
[&] Capture any reference variable by reference
[=] Capture any reference variable by making a copy
[=, &foo] Capture any referenced variable by making a copy, but capture variable foo by reference
[bar]  
[this]  

int x = 4;

auto y = [&r = x, x = x+1]()->int{ r += 2; return x+2;}(); //::x will be 6, y will be 7;

or

auto y = [&r = x, x = x+1]()->int{ r += 2; return x+2;}; // it is not excuted now

y(); // it is executed. ::x is 6

C++ lambda expression的更多相关文章

  1. hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression) 分类: hdoj 2015-06-15 17:47 26人阅读 评论(0) 收藏

    partial sort. first use std::nth_element to find pivot, then use std::stable_partition with the pivo ...

  2. Part 99 Lambda expression in c#

    class Program { static void Main(string[] args) { List<Person> persons = new List<Person> ...

  3. 浅析Java 8新特性Lambda Expression

    什么是Lambda Expression 对于Lambda Expression,我的理解是,它是一个函数表达式,如下: (int x, int y) -> x - y 符号左边定义了函数的输入 ...

  4. Lambda Expression

    Java 8的一个大亮点是引入Lambda表达式,使用它设计的代码会更加简洁.当开发者在编写Lambda表达式时,也会随之被编译成一个函数式接口.下面这个例子就是使用Lambda语法来代替匿名的内部类 ...

  5. Variable used in lambda expression should be final or effectively final

    Lambda与匿名内部类在访问外部变量时,都不允许有修改变量的倾向,即若: final double a = 3.141592; double b = 3.141592; DoubleUnaryOpe ...

  6. JDK 8 - Lambda Expression 的优点与限制

    我们知道 JDK 8 新增了 Lambda Expression 这一特性. JDK 8 为什么要新增这个特性呢? 这个特性给 JDK 8 带来了什么好处? 它可以做什么?不可以做什么? 在这篇文章, ...

  7. Lambda Expression in C#

    1.Expression Expression<Func<double, double>> exp = a => Math.Sin(a); 委托类型Func<dou ...

  8. 关于 lambda expression 返回值的类型转换

    lambda expression(lambda 表达式,$\lambda$ 表达式) 是 C++ 11 引入的特性. 一般而言,lambda 表达式的返回值类型可不指定,而由返回值推断. 需要注意的 ...

  9. 三元運算子回傳lambda expression

    紀錄一下 假如我想要透過三元運算子?: 傳回lambda expression 要明確轉型

随机推荐

  1. 等待资源(wait_resource)解码

    在调查阻塞或死锁时,你可能会遇到等待资源(wait_resource),通常等待的资源是Page或Key: waitresource=“PAGE: 6:3:70133 “waitresource=“K ...

  2. tf serving的使用

    tensorflow_model_server --port=6000 --model_name=text_lstm --model_base_path=/home/guoyingmei/test/t ...

  3. 【Android - 控件】之MD - CardView的使用

    CardView是Android 5.0新特性——Material Design中的一个布局控件,可以通过属性设置显示一个圆角的类似卡片的视图. 1.CardView的属性: app:cardCorn ...

  4. python模块成像库pillow

    python之成像库pillow python提供了python image library图像库,处理图像功能,该库提供了广泛的文件格式支持,如JPEG.PNG.GIF.等,它提供了图像档案.图像显 ...

  5. 【MySQL】数据库课程实验

    数据定义 #mysql --version 查版本号 #mysql -uroot -p #登录 show databases; #查询当前服务存在的数据库 #create database test ...

  6. 用.NET解索尼相机ARW格式照片

    用.NET解索尼相机ARW格式照片 目前常用的照片格式是.jpg,它只能提供8bit的色彩深度,而目前主流的相机都能提供高达12bit-14bit的色彩深度,动态范围和后期处理能力也大大增加,这也是为 ...

  7. UML类图绘制

    UML图简介 含义:UML-Unified Modeling Language 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言 主要模型: 功能模型:从用户的角度展示系统 ...

  8. shell ssh 远程机器 追加文件内容

    在host1上,把下面的两行内容通过ssh追加到host2上的/etc/ca-certificates.conf文件中 I am a student Are you ok ssh host2 &quo ...

  9. [Mathematics][BJTU][Calculus]Detailed explanations and proofs of the Dirac-Abel Discriminant Methods which deal with the conditional convergence

    So, today we will talk about the conditional convergence and two discriminant methods, namely Dirac- ...

  10. luogu P1356 数列的整数性 |动态规划

    题目描述 对于任意一个整数数列,我们可以在每两个整数中间任意放一个符号'+'或'-',这样就可以构成一个表达式,也就可以计算出表达式的值.比如,现在有一个整数数列:17,5,-2,-15,那么就可以构 ...