Emerged since c++11, lambda expression/function is an unnamed function object capable of capturing variables in scope.
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 }
examples:
[](){} //barebone lambda
[](){}() // immediately execute a lambda
auto print = [](const int& n) { n = n+1; std::cout << " " << n; };
std::for_each(v.begin(), v.end(), print); // caller of the lambda expression
std::for_each(v.begin(), v.end(), [](int &n){ n++; });
原文地址:https://www.cnblogs.com/sarah-zhang/p/12075687.html
时间: 2024-10-13 15:02:24