-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexercise7.cpp
77 lines (69 loc) · 2.16 KB
/
exercise7.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
77
#include "city.h"
#include "country.h"
#include "world-util.h"
#include <map>
#include <iostream>
#include <string>
#include <memory>
#include <algorithm>
#include <numeric>
using namespace std;
using namespace world;
map<int,shared_ptr<city>> cities;
map<string,shared_ptr<country>> countries;
template <class E,class T>
struct statistics {
E min_element;
E max_element;
T min;
T max;
int numberOfSamples;
statistics() : numberOfSamples(0) {}
auto add_sample(E sample,T sampleValue){
if (numberOfSamples == 0) {
min = sampleValue;
min_element = sample;
max = sampleValue;
max_element = sample;
}
numberOfSamples++;
if (min > sampleValue) {
min = sampleValue;
min_element = sample;
} else if (max < sampleValue) {
max = sampleValue;
max_element = sample;
}
return *this;
}
};
ostream& operator<<(ostream& out,const statistics<shared_ptr<city>,int>& stats){
if (stats.min_element.get()==nullptr) {
out << "No body lives in this country!" ;
}else{
out << "statistics [ "
<< stats.min_element->name
<< " ( " << stats.min << " )"
<< ", " << stats.max_element->name
<< " ( " << stats.max << " )"
<< " ]";
}
return out;
}
typedef statistics<shared_ptr<city>, int> CityPopulationStatistics;
typedef shared_ptr<city> SharedPtrCity;
int main(int argc, char* argv[]){
create_world();
// Find cities with the min and the max population in each country
for (auto &entry: countries) {
auto this_country = entry.second;
auto country_cities = this_country->cities;
auto statisticsReducer = [](auto&& cityPopulationStatistics,auto &a_city) {
return cityPopulationStatistics.add_sample(a_city,a_city->population);
};
auto populationStatistics = accumulate(country_cities.begin(), country_cities.end(), CityPopulationStatistics(), statisticsReducer);
cout << this_country->name << "'s statistics ==> \t" ;
cout << populationStatistics << endl;
}
return 0;
}