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 要明確轉型
随机推荐
- 图解AQS的设计与实现,手摸手带你实现一把互斥锁!
AQS是并发编程中非常重要的概念,它是juc包下的许多并发工具类,如CountdownLatch,CyclicBarrier,Semaphore 和锁, 如ReentrantLock, ReaderW ...
- <<代码大全>>阅读笔记之一 使用变量的一般事项
一.使用变量的一般事项 1.把变量引用局部化 变量应用局部化就是把变量的引用点尽可能集中在一起,这样做的目的是增加代码的可读性 衡量不同引用点靠近程度的一种方法是计算该变量的跨度(span) 示例 a ...
- 【译】为什么永远都不要使用MongoDB Why You Should Never Use MongoDB
背景 最近在学习DDIA(Designing Data-Intensive Applications)这本分布式领域非常急经典的入门书籍,里面第二章<数据模型与查询语言>,强调了对一对多. ...
- yum运行报错:File "/usr/libexec/urlgrabber-ext-down", line 28
[root@sdw1 bin]# vim /usr/libexec/urlgrabber-ext-down 再次执行yum命令,正常下载
- 安装Linux基本工具
yum install wget httpd-tools vim lrzsz Linux安装wget:yum -y install wget Linux安装vim编辑器:yum -y install ...
- MySQL详解
MySQL详解 什么是数据库 # 用来存储数据的仓库 # 数据库可以在硬盘及内存中存储数据 # 数据库与文件存储数据区别 # 数据库本质也是通过文件来存储数据, 数据库的概念就是系统的管理存储数据的文 ...
- c# 窗体开发3 文件处理技术
以字节形式向磁盘写入数据通常称为字节流(比特流) 常常使用System.Io 常用的类 类 说明 File 提供用于创建.复制.删除.移动和打开文件的静态方法,并协助创建 FileStream 对象. ...
- 【洛谷P1835】素数密度
题目描述: 给定区间[L,R](L≤R≤2147483647,R-L≤1000000),请计算区间中素数的个数. 思路: 暴力: 蒟蒻:哦?绿题?这么水?(便打出下面代码) 这绝对是最容易想到的!但, ...
- 第五章 Unity中的基础光照(1)
[TOC] 渲染总是围绕着一个基础问题:我们如何决定一个像素的颜色?从宏观上来说,渲染包括了两大部分:决定一个像素的可见性,决定这个像素上的光照计算.而光照模型用于决定在一个像素上进行怎样的光照计算. ...
- 本土化App名稱和icon
本土化app名稱這個容易 第一步配置工程需要本土化的語言. 第二步,新建本土化文件,文件名稱是有要求的,文件名字命名为InfoPlist,且必须是这个名字.這樣系統會自動去讀取該文件中的內容 對新建的 ...