-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweek8-app1.cpp
56 lines (43 loc) · 1.26 KB
/
week8-app1.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
#include <iostream>
#include <vector>
// function objects, lambda functions
// containers and iterator protocol/interface
// std::accumulate
struct FunctionObject
{
float multiplier = 10.0f;
float threshold = 15.0f;
auto operator() (float a, float b) const
{
auto k = multiplier*(a + b);
return k > threshold ? k : 0.0f;
// if(k > threshold)
// return k;
// return 0.0f;
}
};
template<typename Container, typename T, typename FUNC>
auto accumulate(const Container& v, T init, FUNC func)
{
for(auto it = v.begin(); it != v.end(); ++it)
init = func(init, *it);
return init;
}
int main(int argc, char* argv[])
{
auto v = std::vector<float>{1.1f, 2.2f, 3.3f};
// auto fo = FunctionObject{};
// auto result = accumulate(v, 0.0f, fo);
auto multiplier = 25.5f;
auto threshold = 15.0f;
auto lambda = [multiplier, threshold](float a, float b) {
auto k = multiplier*(a + b);
return k > threshold ? k : 0.0f;
};
auto result = accumulate(v, 0.0f, lambda);
// auto init = 0;
// for(auto it = v.begin(); it != v.end(); ++it)
// init = init + *it;
std::cout << result << std::endl;
return 0;
}