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,

相关推荐
Smile丶凉轩2 小时前
Qt 界面优化(绘图)
开发语言·数据库·c++·qt
small_wh1te_coder4 小时前
从经典力扣题发掘DFS与记忆化搜索的本质 -从矩阵最长递增路径入手 一步步探究dfs思维优化与编程深度思考
c语言·数据结构·c++·stm32·算法·leetcode·深度优先
hjjdebug6 小时前
constexpr 关键字的意义(入门)
c++·constexpr
虾球xz7 小时前
游戏引擎学习第282天:Z轴移动与摄像机运动
c++·学习·游戏引擎
.小墨迹7 小时前
Apollo学习——planning模块(3)之planning_base
linux·开发语言·c++·学习·自动驾驶
龙湾开发8 小时前
计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 10.增强表面细节(一)过程式凹凸贴图
c++·笔记·学习·3d·图形渲染
德亦周8 小时前
如何在Mac电脑上的VScode去配置C/C++环境
c++·vscode·macos
XiaoyaoCarter8 小时前
每日一道leetcode(新学数据结构版)
数据结构·c++·算法·leetcode·职场和发展·哈希算法·前缀树
八月的雨季 最後的冰吻9 小时前
SIP协议栈--osip源码梳理
linux·服务器·网络·c++·网络协议
fancy1661669 小时前
搜索二维矩阵 II
c++·算法·矩阵