-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexercise6.cpp
76 lines (63 loc) · 2.42 KB
/
exercise6.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
#include "city.h"
#include "country.h"
#include "world-util.h"
#include <map>
#include <iostream>
#include <string>
#include <memory>
#include <numeric>
using namespace std;
using namespace world;
map<int, shared_ptr<city>> cities;
map<string, shared_ptr<country>> countries;
template<class T>
struct statistics {
T minimum;
T maximum;
double sum;
int numberOfSamples;
statistics() : sum(0), numberOfSamples(0) {}
[[maybe_unused]] explicit statistics(T value) : sum(value), numberOfSamples(1), minimum(value), maximum(value) {}
[[nodiscard]] double average() const {
if (numberOfSamples == 0) return 0.0;
return sum / numberOfSamples;
}
};
template<class T>
ostream &operator<<(ostream &out, const statistics<T> &stats) {
out << "statistics [ number of samples= " << stats.numberOfSamples
<< ", minimum=" << stats.minimum
<< ", maximum=" << stats.maximum
<< ", sum=" << stats.sum
<< ", average=" << stats.average()
<< " ]";
return out;
}
using statistics_int = statistics<int>;
using statistics_int_ptr = shared_ptr<statistics_int>;
int main() {
create_world();
// Find the min, the max, and the average population at each continent
auto statisticsReducer = [](auto &&continentStatistics, auto &entry) {
auto country = entry.second;
auto continent = country->continent;
auto continentStatisticsIterator = continentStatistics.find(continent);
if (continentStatisticsIterator == continentStatistics.end())
continentStatistics[continent] = make_shared<statistics_int>(statistics_int(country->population));
else {
auto continentStat = continentStatisticsIterator->second;
auto countryPopulation = country->population;
if (continentStat->minimum > countryPopulation) continentStat->minimum = countryPopulation;
if (continentStat->maximum < countryPopulation) continentStat->maximum = countryPopulation;
continentStat->sum += countryPopulation;
continentStat->numberOfSamples++;
}
return continentStatistics;
};
auto continentStatistics = accumulate(countries.begin(), countries.end(), map<string, statistics_int_ptr>(),
statisticsReducer);
for (auto &entry: continentStatistics) {
cout << entry.first << ": " << *(entry.second) << endl;
}
return 0;
}