Lambda expressions in C++ (C++ 中的 lambda 表达式)

Lambda expressions in C++ {C++ 中的 lambda 表达式}

  • [1. Parts of a lambda expression (Lambda 表达式的各个部分)](#1. Parts of a lambda expression (Lambda 表达式的各个部分))

    • [1.2. Parameter list (Optional)](#1.2. Parameter list (Optional))
  • References

    lambda /ˈlæm.də/:the 11th letter of the Greek alphabet (希腊语字母表的第 11 个字母)

https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp

1. Parts of a lambda expression (Lambda 表达式的各个部分)

This illustration shows the parts of lambda syntax:

  1. capture clause (Also known as the lambda-introducer in the C++ specification)

  2. parameter list Optional (可选) (Also known as the lambda declarator)

  3. mutable specification Optional (可选)

  4. exception-specification Optional (可选)

  5. trailing-return-type Optional (可选)

  6. lambda body

1.2. Parameter list (Optional)

Lambdas can both capture variables and accept input parameters.

lambda 既可以捕获变量,也可以接受输入参数。

A parameter list (lambda declarator in the Standard syntax) is optional and in most aspects resembles the parameter list for a function.

参数列表 (在标准语法中称为 lambda 声明符) 是可选的,它在大多数方面类似于函数的参数列表。

复制代码
#include <stdio.h>

int main(void) {

	auto add_func = [](const int first, const int second) {
		return first + second;
	};

	const int result = add_func(2, 3);

	printf("result = %d\n", result);

	return 0;
}

result = 5
请按任意键继续. . .

lambda could own default arguments.

复制代码
#include <stdio.h>

int main(void) {

	auto add_func = [](const int first, const int second = 12) {
		return first + second;
	};

	const int result = add_func(3);

	printf("result = %d\n", result);

	return 0;
}

result = 15
请按任意键继续. . .

References

1\] Yongqiang Cheng,

相关推荐
ShineWinsu2 小时前
对于数据结构:堆的超详细保姆级解析——下(堆排序以及TOP-K问题)
c语言·数据结构·c++·算法·面试·二叉树·
_OP_CHEN2 小时前
C++进阶:(五)map系列容器的全面解析
开发语言·c++·map·红黑树·stl容器·键值对·mapoj题
hetao17338372 小时前
ZYZ28-NOIP模拟赛-Round4 hetao1733837的record
c++·算法
大米粥哥哥2 小时前
c++ libcurl报错Send failed since rewinding of the data stream failed【已解决】
开发语言·c++·http·curl·rewind
woshimyc2 小时前
ESP32连接ThingsCloud上传设备数据(智慧小灯)
c++·物联网
Maple_land3 小时前
Linux复习:系统调用与fork
linux·运维·服务器·c++·centos
墨雪不会编程3 小时前
C++的基础语法篇一 ——命名空间
开发语言·c++
火山上的企鹅3 小时前
Qt C++ 软件开发工程师面试题
c++·qt·面试
沐怡旸4 小时前
【穿越Effective C++】条款16:成对使用new和delete时要采用相同形式——内存管理的精确匹配原则
c++·面试
z20348315204 小时前
我与C++的故事
开发语言·c++·c++40周年