-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
53 lines (47 loc) · 1.16 KB
/
main.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
// Some awesome note.
// http://cppcodereviewers.com/readable-for_each-loop-for-maps/
#include <iostream>
#include <map>
#include <vector>
#include <numeric>
// DATA
auto student_points = std::map<std::string, std::vector<int>> {
{"Filip", {1,2,3}},
{"Adrian", {5,6,7}}
};
// BEFORE
namespace before
{
void sum()
{
for (const auto& e : student_points)
{
std::cout << "Sum of points for student: '" << e.first << "' is "
<< std::accumulate(e.second.begin(), e.second.end(), 0) << std::endl;
}
}
}
// AFTER
namespace after
{
template <typename Range, typename Fun>
void map_for_each(const Range& range, Fun f)
{
for(const auto& e : range)
f(e.first, e.second);
}
void sum()
{
map_for_each(student_points, [](auto& student, auto& points){
std::cout << "Sum of points for student: '" << student << "' is "
<< std::accumulate(points.begin(), points.end(), 0) << std::endl;
});
}
}
int main()
{
std::cout << "---- BEFORE" << std::endl;
before::sum();
std::cout << "---- AFTER" << std::endl;
after::sum();
}