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 要明確轉型
随机推荐
- Reactor和Proactor模型
背景 前面介绍了I/O多路复用模型,那有了I/O复用,有了epoll已经可以使服务器并发几十万连接的同时,还能维持比较高的TPS,难道还不够吗?比如现在在使用epoll的时候一般都是起个任务,不断的去 ...
- java引用知识
最近从新拜读<深入理解Java虚拟机:JVM高级特性与最佳实践>这本书,看到有关引用的相关知识,以前没有好的习惯,这次看完在博客上记录下 引用:如果reference类型中的数据存储的数值 ...
- day01_爬虫和数据
1.什么是爬虫 1.1.爬虫的定义 脚本,程序--->自动抓取万维网上信息的程序. 1.2.爬虫的分类 2.1.通用爬虫 通用网络爬虫 是 捜索引擎抓取系统(Baidu.Google ...
- 浏览器中的 Event Loop
当我们执行 JS 代码的时候其实就是往执行栈中放入函数,那么遇到异步代码的时候该怎么办?其实当遇到异步的代码时,会被挂起并在需要执行的时候加入到 Task(有多种 Task) 队列中.一旦执行栈为空, ...
- 阿里P7整理“硬核”面试文档:Java基础+数据库+算法+框架技术等
现在的程序员越来越多,大部分的程序员都想着自己能够进入大厂工作,但每个人的能力都是有差距的,所以并不是人人都能跨进BATJ.即使如此,但身在职场的我们一刻也不能懈怠,既然对BATJ好奇,那么就要朝这个 ...
- 英语口语考试资料Language learning
"Learning a language is easy. Even a child can do it!" Most adults who are learning a seco ...
- PHP自动发红包代码示例
<?php header('Content-type:text'); define("TOKEN", "weixin"); $wechatObj = ne ...
- DS1302时钟芯片驱动程序
/***************************************************************************** FileName : DS1302.c F ...
- alloc 和 init都做了什么验证。
结论: alloc负责分配内存和创建对象对应的isa指针: init只是返回alloc生成的对象. 所以alloc后,多次调用init,返回的对象是同一个! 代码如下: // // main.m / ...
- 思科CISCO ASA 5521 防火墙 Ipsec 配置详解
版本信息: Cisco Adaptive Security Appliance Software Version 9.9(2) Firepower Extensible Operating Syste ...