-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambdas.cpp
More file actions
179 lines (165 loc) · 3.44 KB
/
lambdas.cpp
File metadata and controls
179 lines (165 loc) · 3.44 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
// using Comparator = bool(*)(int, int);
template <typename T, int size, typename Comparator>
void Sort(T (&arr)[size], Comparator comp)
{
for (int i = 0; i < size - 1; ++i)
{
for (int j = 0; j < size - 1; ++j)
{
if (comp(arr[j], arr[j + 1]))
{
T temp = std::move(arr[j]);
arr[j] = std::move(arr[j + 1]);
arr[j + 1] = std::move(temp);
}
}
}
}
bool Comp(int x, int y)
{
return x > y;
}
bool Comp1(int x, int y)
{
return x < y;
}
struct Comp2
{
bool operator()(int x, int y)
{
return x > y;
}
};
template <typename T, int size, typename Callback>
void ForEach(T (&arr)[size], Callback operation)
{
for (int i = 0; i < size - 1; ++i)
{
operation(arr[i]);
}
}
template <typename T>
struct __Unnamed
{
int offset;
__Unnamed(int off) : offset(off)
{
}
void operator()(T &x)
{
x += offset;
++offset;
}
};
class Product
{
std::string name;
float price;
public:
Product(const std::string &n, float p) : name(n), price(p)
{
}
void AssignFinalPrice()
{
float taxes[]{12, 5, 5};
float basePrice{price};
// Capture this
ForEach(taxes, [basePrice, this](float tax)
{
float taxedPrice = basePrice * tax / 100;
price += taxedPrice; });
}
float GetPrice() const
{
return price;
}
};
int main()
{
atexit([]()
{ std::cout << "Program is exitting..." << std::endl; });
/*Product p{ "Watch", 500 };
p.AssignFinalPrice();
std::cout << p.GetPrice() << std::endl;*/
// Lambda within a lambda
[](int x)
{
x *= 2;
[](int x)
{
std::cout << x << std::endl;
}(x);
}(5);
__Unnamed<int> n(3);
int x = 5;
n(x);
int arr[]{1, 6, 8, 4, 0};
ForEach(arr, [](auto x)
{ std::cout << x << " "; });
std::cout << std::endl;
int offset = 0;
/*ForEach(arr, [offset](auto &x) {
x += offset;
});*/
// ForEach(arr, [offset](auto &x)mutable {
// x += offset;
// ++offset;
// });
int sum{};
ForEach(arr, [&, offset](auto &x)
{ sum += x; });
std::cout << "Sum:" << sum << std::endl;
ForEach(arr, [](auto x)
{ std::cout << x << " "; });
std::cout << std::endl;
return 0;
}
int mainOld()
{
int arr[]{1, 6, 8, 4, 0};
for (auto x : arr)
{
std::cout << x << " ";
}
std::cout << std::endl;
auto comp = [](auto x, auto y)
{
return x > y;
};
Sort(arr, comp);
for (auto x : arr)
{
std::cout << x << " ";
}
std::cout << std::endl;
}
// lambda basics
// Possible implementation of a lambda expression as a function object
template <typename T>
struct Unnamed
{
T operator()(T x, T y) const
{
return x + y;
}
};
int mainBasic()
{
// Compiler automatically creates a function object
auto fn = []()
{
std::cout << "Welcome to Lambda expressions" << std::endl;
};
fn();
std::cout << typeid(fn).name() << std::endl;
// Generic/polymorphic lambda
// <optional specifiers>
auto sum = [](auto x, auto y) // mutable// //noexcept//(false)
{
return x + y; // ^^^^ ^^^^
};
Unnamed<int> n;
std::cout << "Sum is:" << sum(5.5f, 2.2f) << std::endl;
return 0;
}