-
Notifications
You must be signed in to change notification settings - Fork 12
/
lambda.cpp
executable file
·116 lines (91 loc) · 2.7 KB
/
lambda.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <algorithm>
#include <iostream>
#include <vector>
/*
syntax:
[ captures ] <tparams>(optional)(c++20) ( params ) specifiers exception attr ->
ret requires(optional)(c++20) { body } [ captures ] ( params ) -> ret { body }
[ captures ] ( params ) { body }
[ captures ] { body }
[ = ] this means passing parameters by value, but they are read only, you need
to use mutable to modify read only
include all parameters
lambda = [](void) { body };
return type is also void
lambda = [](void) -> void {}
*/
template <typename T> void printVec(std::vector<T> values) {
for (auto value : values)
std::cout << value << std::endl;
}
void increaser(int &i) { // function:
i = i + 1;
}
void transformExample() {
std::vector<int> x = {1, 2, 3};
std::vector<int> y(x.size(), 0);
std::vector<int> z(x);
std::transform(x.begin(), x.end(), y.begin(), [](int a) { return a += 1; });
std::for_each(z.begin(), z.end(), [&](int a) { return a = a + 1; });
// std::for_each(z.begin(), z.end(), increaser);
printVec(y);
printVec(z);
}
void count_ifExample() {
std::vector<int> x = {1, 2, 1, 4, 5, 1, 3};
int ones;
ones = std::count_if(x.begin(), x.end(), [](int &a) { return a == 1; });
std::cout << "Total number of ones in the vector is: " << ones << std::endl;
auto f = [](int &a) { return a == 1; };
std::count_if(x.begin(), x.end(), f);
ones = std::count_if(x.begin(), x.end(), [](int &a) { return a == 1; });
std::cout << "Total number of ones in the vector is: " << ones << std::endl;
}
bool sortFunc(int x, int y) { return (x < y); }
void labmdaExample() {
std::vector<int> v1;
for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})
v1.push_back(n);
// inline
std::sort(v1.begin(), v1.end(), [](int x, int y) { return (x < y); });
// outside
v1.clear();
for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})
v1.push_back(n);
auto cmp = [](int x, int y) { return (x < y); };
std::sort(v1.begin(), v1.end(), cmp);
// outside as a function
v1.clear();
for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})
v1.push_back(n);
std::sort(v1.begin(), v1.end(), sortFunc);
}
void lambdaPassByValue() {
int a, b;
a = 10;
b = 12;
std::cout << a << std::endl;
auto lambda = [=]() mutable { std::cout << ++a << std::endl; };
lambda();
std::cout << a << std::endl;
}
void lambdaPassByRef() {
int a, b;
a = 10;
b = 12;
std::cout << a << std::endl;
auto lambda = [&]() {
std::cout << ++a << std::endl;
b++;
};
lambda();
std::cout << a << std::endl;
}
int main() {
// transformExample();
// count_ifExample();
// lambdaPassByValue();
lambdaPassByRef();
// auto lambda = []() { std::cout << "Code within a lambda expression" <<
// std::endl; }; lambda();
}