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/

相关推荐
Dream it possible!1 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴1 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程1 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
澄澈天空3 小时前
C++ MFC添加RichEditControl控件后,程序启动失败
c++·mfc
Lzc7743 小时前
C++初阶——简单实现vector
c++·简单实现vector
一个小白14 小时前
C++——list模拟实现
开发语言·c++
程序员老舅4 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
靡不有初1115 小时前
CCF-CSP第18次认证第一题——报数【两个与string相关的函数的使用】
c++·学习·ccfcsp
cookies_s_s6 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
不想编程小谭7 小时前
力扣LeetCode: 2506 统计相似字符串对的数目
c++·算法·leetcode