-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.cpp
More file actions
54 lines (46 loc) · 938 Bytes
/
Copy pathlambda_function.cpp
File metadata and controls
54 lines (46 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <functional> // Needed for std:: function
using namespace std;
void myFunction(function<void()> func)
{
func();
func();
}
int main()
{
auto message = []()
{
cout << "Hello World!\n";
};
// lambda with parameters
auto add = [](int a, int b)
{
return a + b;
};
message();
cout << add(3, 4) << endl;
myFunction(message);
for (int i = 1; i <= 3; i++)
{
auto show = [i]()
{
cout << "Number : " << i << endl;
};
show();
}
int i = 10;
auto show = [i]() { // You can use the [ ] brackets to give a lambda
cout << i; // access to variables outside of it.
}; // This is called the capture clause.}
show();
cout << endl;
// capture by refernce
int x = 30;
auto show2 = [x]()
{
cout << x;
};
x = 20;
show2();
return 0;
}