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. Chapter 01—Introduction to R

    1.getwd():list the current working directory. (即获得当前工作路径) 2.setwd("mydirectory"):change th ...

  2. __dict__和dir()的区别

    __dict__和dir()的区别 dir() 一般用来查看模块的属性 __dict__从某方面上来说是dir()的子集 可以直接打印dir(),显示的是当前执行文件所有的属性 __dict__ __ ...

  3. Mac SourceTree配置Beyond Compare

    一   首先下载正版的Beyond Compare 地址:https://www.scootersoftware.com/download.php 二   如果bin文件夹下没有bcomp,打开终端命 ...

  4. 解决mysql java.sql.SQLException: The server time zone value‘XXXXXX' is unrecognized or represents...

    解决 java.sql.SQLException: The server time zone value 'XXXXXX' is unrecognized or represents more tha ...

  5. unity3D 游戏物体同时绑定单击、双击事件

    前言 在unity中我们常用的获取鼠标点击的方法有 在3D场景中,一般用在Update方法中,每一帧调用 void Update(){ )){ Debug.log("鼠标左键点击" ...

  6. 第九次作业——DFA最小化,语法分析初步

    老师:MissDu 提交作业 1.将DFA最小化:教材P65 第9题 答: 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 3.自上而下语法分析,回溯产生的原因是 ...

  7. 共享共建会让中国的5G加速吗?

    9月9号,中国联通正式公告,已与中国电信签署<5G网络共建共享框架合作协议书>,将在全国范围内合作共建5G接入网络. 这则消息堪称爆炸性新闻,但却看不到什么深度分析,评论文章除了强调&qu ...

  8. milvus安装及其使用教程

    milvus 简介 milvus是干什么的?通俗的讲,milvus可以让你在海量向量库中快速检索到和目标向量最相似的若干个向量,这里相似度量标准可以是内积或者欧式距离等.借用官方的话说就是: Milv ...

  9. luogu P1582 倒水 |数学

    题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把一个瓶子的水全部倒 ...

  10. IOS UIAlertView(警告框)方法总结

    转自:my.oschina.net/u/2340880/blog/408873?p=1 IOS中UIAlertView(警告框)常用方法总结 一.初始化方法 - (instancetype)initW ...