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 要明確轉型
随机推荐
- 【集训Day2 哈希表】【NHOI2015】【Luogu P2421】差
LuoguP2421 原题来自NHOI2015 [解题思路] 本题的解题方法有三种,一种为枚举减数,二分查找被减数.第二种为利用数据单调性用尺取法进行查找,第三种为运用哈希表以快速查找数据. [解题反 ...
- Java多线程——线程间通信
Java多线系列文章是Java多线程的详解介绍,对多线程还不熟悉的同学可以先去看一下我的这篇博客Java基础系列3:多线程超详细总结,这篇博客从宏观层面介绍了多线程的整体概况,接下来的几篇文章是对多线 ...
- 附011.Kubernetes-DNS及搭建
一 Kubernetes DNS介绍 1.1 Kubernetes DNS发展 作为服务发现机制的基本功能,在集群内需要能够通过服务名对服务进行访问,因此需要一个集群范围内的DNS服务来完成从服务名到 ...
- P1035 级数求和
题目描述 已知:S_n= 1+1/2+1/3+…+1/nSn=1+1/2+1/3+…+1/n.显然对于任意一个整数KK,当nn足够大的时候,S_nSn大于KK. 现给出一个整数KK(1 \le k ...
- 将String类型转换为int整数类型
示例如下: public class demo { public static void main(String[] args) { String s="10"; 6 7 //St ...
- node_export 安装
目录 安装部署 环境准备 下载安装 启动测试 安装部署 环境准备 主机名 角色 IP 系统版本 内核版本 es01.k8s.com node01 10.0.20.11 CentOS 7.5 5.1.4 ...
- 上传一个项目到GitHub
在github上创建hello的仓库 上传本地项目文件 echo "# hello" >> README.md git init git add README.md g ...
- js实现冒泡排序(bubble sort)快速排序(quick sort)归并排序(merge sort)
排序问题相信大家都比较熟悉了.用js简单写了一下几种常用的排序实现.其中使用了es6的一些语法,并且不仅限于数字--支持各种类型的数据的排序.那么直接上代码: function compare (a, ...
- NoSql中的CAP原则
C:一致性 .A:可用性.P:分区容错性 Partition tolerance(分区容错性): 大多数分布式系统都分布在多个子网络.每个子网络就叫做一个区(partition).分区容错的意思是,区 ...
- iOS包管理工具Cocoapods的安装与使用
转自:http://www.sxt.cn/u/10014/blog/6448 在我们开发移动应用的时候,一般都会使用到第三方工具,而由于第三方类库的种类繁多,我们在项目中进行管理也会相对麻烦,所以此时 ...