-
Notifications
You must be signed in to change notification settings - Fork 4
/
drill21-2.cpp
54 lines (46 loc) · 1.26 KB
/
drill21-2.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
#include <iostream>
#include <map>
#include <numeric>
#include <string>
using namespace std;
template<class K, class V>
void print_map(const map<K, V>& m) {
for (const auto& p : m)
cout << '(' << p.first << ',' << p.second << ')' << endl;
}
template<class K, class V>
void read_pairs(map<K, V>& m) {
for (pair<K, V> p; cin >> p.first >> p.second;)
m.insert(p);
}
int main() {
map<string, int> msi = {
{"apple", 5},
{"banana", 7},
{"car", 30000},
{"dog", 15},
{"education", 12},
{"football", 10},
{"guitar", 500},
{"house", 500000},
{"internet", 20},
{"jacket", 50}
};
cout << "(name, value) pairs in msi:\n";
print_map(msi);
msi.erase("car");
msi.erase("guitar");
cout << "\nErase pairs:\n";
print_map(msi);
read_pairs(msi);
cout << "\nRead pairs from input:\n";
print_map(msi);
int sum = accumulate(msi.begin(), msi.end(), 0, [](int x, const pair<string, int>& p) { return x + p.second; });
cout << "\nSum of the values in msi is " << sum << endl;
map<int, string> mis;
for (const auto& p : msi)
mis.emplace(p.second, p.first);
cout << "\n(value, name) pairs in mis:\n";
print_map(mis);
return 0;
}