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. Spring事务笔记

    1:在同一个类中,如果A方法有事务,B方法也有事务(propagation = Propagation.REQUIRES_NEW),如下代码所示: @Override@Transactionalpub ...

  2. 附012.Kubeadm部署高可用Kubernetes

    一 kubeadm介绍 1.1 概述 参考<附003.Kubeadm部署Kubernetes>. 1.2 kubeadm功能 参考<附003.Kubeadm部署Kubernetes& ...

  3. python字符串的特性及相关应用

    一.字符串定义 字符串是 Python 中最常用的数据类型.用单引号(' '),双引号(" ")或者三引号(''' ''')括起来的数据称为字符串(其中,使用三引号的字符串可以横跨 ...

  4. 2019-2020-6 20199317《Linux内核原理与分析》第六周作业

    第5章  系统调用的三层机制(下) 1  给MenuOS增加命令         首先进入LinuxKernel目录下,用rm -rf menu强制删除当前的menu目录,然后用git clone重新 ...

  5. 《手把手教你》系列练习篇之9-python+ selenium自动化测试 -番外篇 - 最后一波啊!!!(详细教程)

    1. 简介 本来上一篇就是练习篇的最后一篇文章了,但是有的小伙伴私下反映说是做了那么多练习,没有一个比较综合的demo练练手.因此宏哥在这里又补存了一些常见的知识点进行练习,在文章最后也通过实例给小伙 ...

  6. ActiveMQ配置策略

    1.消息发送 1.异步发送 消息生产者使用持久(persistent)传递模式发送消息的时候,Producer.send() 方法会被阻塞,直到 broker 发送一个确认消息给生产者,这个确认消息暗 ...

  7. 脚本shell每小时递增运行task

    下面 hello 是开始时间, world 是结束时间 #!/bin/bash START=$(date +%s); hello="20160911 00" world=" ...

  8. ios 测试网络是否连接

    转自:http://blog.csdn.net/lwq421336220/article/details/16982857 - (BOOL) connectedToNetwork { //创建零地址, ...

  9. border-radius:50%和100%的区别

    border-radius 值类型-百分比 border-radius的值是百分比的话,就相当于盒子的宽度和高度的百分比. 我们知道在一个正方形内做一个面积最大的圆形,这个圆的半径就为正方形边长的一半 ...

  10. Spring底层源码分析

    Spring 运行原理 Spring 启动时读取应用程序提供的 Bean 配置信息,并在 Spring 容器中生成一份相应的Bean 配置注册表,然后根据这张注册表实例化 Bean,装配好 Bean ...