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, https://yongqiang.blog.csdn.net/

相关推荐
2401_8414956410 小时前
【操作系统】进程同步与互斥实验报告
c++·算法·操作系统·进程·并发·同步·互斥
fqbqrr10 小时前
2607C++,soui与安卓
c++·soui
fqbqrr13 小时前
2607C++,使用微软detours勾挂工具
c++
蓝悦无人机16 小时前
C++基础 — 函数总结
开发语言·c++
我不是懒洋洋18 小时前
从零实现一个分布式监控:Prometheus的核心设计
c++
An_s18 小时前
c++对接pdfium(一)win系统篇
开发语言·c++
众少成多积小致巨18 小时前
C++ 规范参考(上)
c++
一拳一个呆瓜1 天前
【STL】iostream 编程:使用提取运算符
c++·stl
我不是懒洋洋1 天前
从零实现一个分布式日志平台:ELK的核心设计
c++
c238561 天前
下篇:回溯与剪枝的「智慧寻宝人」——DFS 进阶与网格 / 图论应用
c++·深度优先·图论·剪枝