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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
// 事件处理系统
class Button
{
public:
using ClickHandler = std::function<void()>;
private:
std::string m_Name;
ClickHandler m_OnClick;
public:
Button(const std::string& name) : m_Name(name) {}
void SetOnClick(const ClickHandler& handler)
{
m_OnClick = handler;
}
void Click()
{
std::cout << "Button '" << m_Name << "' clicked!" << std::endl;
if (m_OnClick)
{
m_OnClick();
}
}
};
// 数据处理管道
class DataPipeline
{
public:
using Processor = std::function<int(int)>;
private:
std::vector<Processor> m_Processors;
public:
void AddProcessor(const Processor& processor)
{
m_Processors.push_back(processor);
}
std::vector<int> Process(const std::vector<int>& input)
{
std::vector<int> result = input;
for (const auto& processor : m_Processors)
{
std::transform(result.begin(), result.end(), result.begin(), processor);
}
return result;
}
};
// 配置驱动的行为
class ConfigurableProcessor
{
public:
using FilterPredicate = std::function<bool(int)>;
using TransformFunction = std::function<int(int)>;
private:
FilterPredicate m_Filter;
TransformFunction m_Transform;
public:
void SetFilter(const FilterPredicate& filter)
{
m_Filter = filter;
}
void SetTransform(const TransformFunction& transform)
{
m_Transform = transform;
}
std::vector<int> Process(const std::vector<int>& input)
{
std::vector<int> result;
for (int value : input)
{
if (!m_Filter || m_Filter(value))
{
int transformed = m_Transform ? m_Transform(value) : value;
result.push_back(transformed);
}
}
return result;
}
};
int main()
{
std::cout << "=== Practical Lambda Applications ===" << std::endl;
// 1. 事件处理
std::cout << "1. Event Handling:" << std::endl;
Button saveButton("Save");
Button loadButton("Load");
saveButton.SetOnClick([]() {
std::cout << " Saving file..." << std::endl;
});
loadButton.SetOnClick([]() {
std::cout << " Loading file..." << std::endl;
});
saveButton.Click();
loadButton.Click();
// 2. 数据处理管道
std::cout << "\n2. Data Processing Pipeline:" << std::endl;
DataPipeline pipeline;
// 添加处理步骤
pipeline.AddProcessor([](int x) { return x * 2; }); // 乘以2
pipeline.AddProcessor([](int x) { return x + 10; }); // 加10
pipeline.AddProcessor([](int x) { return x * x; }); // 平方
std::vector<int> input = {1, 2, 3, 4, 5};
auto output = pipeline.Process(input);
std::cout << "Input: ";
for (int val : input) std::cout << val << " ";
std::cout << std::endl;
std::cout << "Output: ";
for (int val : output) std::cout << val << " ";
std::cout << std::endl;
// 3. 配置驱动的处理
std::cout << "\n3. Configurable Processing:" << std::endl;
ConfigurableProcessor processor;
// 只处理偶数
processor.SetFilter([](int x) { return x % 2 == 0; });
// 转换为立方
processor.SetTransform([](int x) { return x * x * x; });
std::vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto filtered = processor.Process(data);
std::cout << "Original: ";
for (int val : data) std::cout << val << " ";
std::cout << std::endl;
std::cout << "Filtered and transformed: ";
for (int val : filtered) std::cout << val << " ";
std::cout << std::endl;
// 4. 排序和搜索的自定义逻辑
std::cout << "\n4. Custom Sorting and Searching:" << std::endl;
struct Person
{
std::string name;
int age;
double salary;
};
std::vector<Person> people = {
{"Alice", 30, 75000},
{"Bob", 25, 60000},
{"Charlie", 35, 80000},
{"Diana", 28, 70000}
};
// 按薪水排序
std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
return a.salary > b.salary;
});
std::cout << "Sorted by salary (descending):" << std::endl;
for (const auto& person : people)
{
std::cout << " " << person.name << ": $" << person.salary << std::endl;
}
// 查找年龄大于30的第一个人
auto found = std::find_if(people.begin(), people.end(), [](const Person& p) {
return p.age > 30;
});
if (found != people.end())
{
std::cout << "First person over 30: " << found->name << " (age " << found->age << ")" << std::endl;
}
return 0;
}
|