C++ lambda expression
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的更多相关文章
- 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 ...
- Part 99 Lambda expression in c#
class Program { static void Main(string[] args) { List<Person> persons = new List<Person> ...
- 浅析Java 8新特性Lambda Expression
什么是Lambda Expression 对于Lambda Expression,我的理解是,它是一个函数表达式,如下: (int x, int y) -> x - y 符号左边定义了函数的输入 ...
- Lambda Expression
Java 8的一个大亮点是引入Lambda表达式,使用它设计的代码会更加简洁.当开发者在编写Lambda表达式时,也会随之被编译成一个函数式接口.下面这个例子就是使用Lambda语法来代替匿名的内部类 ...
- Variable used in lambda expression should be final or effectively final
Lambda与匿名内部类在访问外部变量时,都不允许有修改变量的倾向,即若: final double a = 3.141592; double b = 3.141592; DoubleUnaryOpe ...
- JDK 8 - Lambda Expression 的优点与限制
我们知道 JDK 8 新增了 Lambda Expression 这一特性. JDK 8 为什么要新增这个特性呢? 这个特性给 JDK 8 带来了什么好处? 它可以做什么?不可以做什么? 在这篇文章, ...
- Lambda Expression in C#
1.Expression Expression<Func<double, double>> exp = a => Math.Sin(a); 委托类型Func<dou ...
- 关于 lambda expression 返回值的类型转换
lambda expression(lambda 表达式,$\lambda$ 表达式) 是 C++ 11 引入的特性. 一般而言,lambda 表达式的返回值类型可不指定,而由返回值推断. 需要注意的 ...
- 三元運算子回傳lambda expression
紀錄一下 假如我想要透過三元運算子?: 傳回lambda expression 要明確轉型
随机推荐
- 【Luogu P5490】扫描线
Luogu P5490 作为一道模板题让我卡了一个月…… 对于线段树+离散化新手而言这实在是太难了…… 有关离散化: 可以查看这一篇文章:https://www.jianshu.com/p/93476 ...
- Xamarin.Forms学习系列之Syncfusion 制作图形报表
Syncfusion是一家微软生态下的第三方组件/控件供应商,除了用于HTML5和JavaScript的控件外,他们产品还涉及如下领域: WEB ASP.NET MVC ASP.NET WebForm ...
- Chapter 05—Advanced data management(Part 1)
一. R的数学函数,统计函数及字符处理函数 例01:一道实际应用题 一组学生其数学,科学和英语的成绩如下表: 任务:根据成绩,决定对每个学生的单独指导: 前20%的学生的成绩为A,次之为B,以此类推: ...
- VLAN实验1(VLAN基础配置及Access接口)
本实验基于<HCNA网 络技术实验指南> 本实验使用eNSP软件 原理概述: 早期的局域网技术是基于总线型结构的.总线型拓扑结构是由一根单电缆连接着所 有主机,这种局域网技术存在着冲突域问 ...
- .Net Core的API网关Ocelot的使用(二)[负载,限流,熔断,Header转换]
网关的负载均衡 当下游拥有多个节点的时候,我们可以用DownstreamHostAndPorts来配置 { "UpstreamPathTemplate": "/Api_A ...
- 普通莫队--洛谷P1997 【faebdc的烦恼】
离散化+莫队 cnt数组表示某个颜色出现的次数 sum数组表示某个数量出现的颜色种类 其它细节问题就按照莫队的模板来的 #include<cstdio> #include<algor ...
- LightOJ 1253 Misere NIM(反NIM博弈)
Alice and Bob are playing game of Misère Nim. Misère Nim is a game playing on k piles of stones, eac ...
- ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship
There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry th ...
- CoderForces985F-Isomorphic Strings
F. Isomorphic Strings time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- DRF Django REST framework 之 频率,响应器与分页器组件(六)
频率组件 频率组件类似于权限组件,它判断是否给予请求通过.频率指示临时状态,并用于控制客户端可以向API发出的请求的速率. 与权限一样,可以使用多个调节器.API可能会对未经身份验证的请求进行限制,而 ...