-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path60_Lambda_Expression.cpp
More file actions
76 lines (55 loc) · 1.6 KB
/
60_Lambda_Expression.cpp
File metadata and controls
76 lines (55 loc) · 1.6 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <math.h>
using namespace std;
/*
Lambda expression allows us to define anonymous function objects. Lambda function is a small function we can write directly in codes. It's useful when we need a quick function without
naming it or declaring it separately.
Syntax:
auto name = [capture] (parameters) {codes};
Note: Like normal functions, lambda expressions can also have a return type.
Example:
int main(){
auto message = [](){cout << "Lambda" << endl;} //Create a lambda function and assign it to a variable name (message) without parameters that prints "Lambda".
message();
}
*/
//Passing lambdas to functions
#include <functional>
void myFunc(function<void()> func){
func();
func();
func();
}
int main(){
auto add = [] (int a, int b){
return a+b;
};
int sum;
sum = add(1234, 4567);
cout << sum << endl;
auto multiple = [] (int a, int b) -> double{
return a*b;
};
int mtp;
mtp = multiple(2222, 3333);
cout << mtp << endl;
cout << endl;
//Lambdas in loops
for (int i=0; i<4; i++){
auto print = [i](){
cout << "Number " << i << endl;
};
print();
}
cout << endl;
//Access to variables outside of lambdas
int num1 = 70;
int num2 = 120;
auto myLambda = [num1, num2](){ //num1 and num2 are read-only
cout << sin(num1) + cos(num2) << endl;
};
myLambda();
cout << endl;
//Using lambdas as an argument
myFunc(myLambda);
}