-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexercise5.cpp
42 lines (34 loc) · 1.19 KB
/
exercise5.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
#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;
int main() {
create_world();
auto continentCountriesReducer = [](auto &&continentCountries, auto &entry) {
auto country = entry.second;
auto continent = country->continent;
auto not_found = continentCountries.end();
auto iterator = continentCountries.find(continent);
if (iterator == not_found) continentCountries[continent] = vector<shared_ptr<world::country>>();
continentCountries[continent].push_back(country);
return continentCountries;
};
auto countriesOfContinents = accumulate(countries.begin(), countries.end(),
map<string, vector<shared_ptr<country>>>(), continentCountriesReducer);
for (auto &entry: countriesOfContinents) {
cout << entry.first << endl;
for (auto &eachCountry: entry.second) {
cout << *eachCountry << endl;
}
cout << endl;
}
return 0;
}