#include <iostream>
using namespace std
;
int main()
{
int a
= 5, b
= 7;
auto total
= [](int x
, int y
)->int {return x
+ y
; };
cout
<< total(a
, b
)<<endl
;
auto fun1
= [=] {return a
+ b
; };
cout
<< fun1() << endl
;
auto fun2
= [&](int c
) {b
= a
+ c
; a
= 1; };
fun2(3);
cout
<< a
<<" "<< b
<< endl
;
a
= 5; b
= 7;
auto fun3
= [=, &b
](int c
) mutable {b
= a
+ c
; a
= 1; };
fun3(3);
cout
<< a
<< " " <<b
<< endl
;
a
= 5; b
= 7;
auto fun4
= [=](int x
, int y
) mutable->int {a
+= x
; b
+= y
; return a
+ b
; };
int t
= fun4(10, 20);
cout
<< t
<< endl
;
cout
<< a
<<" "<< b
<< endl
;
return 0;
}
Output:
12
12
1 8
5 8
42
5 7
转载请注明原文地址:https://tech.qufami.com/read-1711.html