C++ lambda expression
程序员文章站
2023-11-02 15:08:58
Emerged since c++11, lambda expression/function is an unnamed function object capable of capturing variables in scope. 1. syntax of a lambda expressio ......
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++; });
上一篇: 青岛的啤酒种类,不知道简直白喝了
下一篇: springIOC及设计模式