-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunordered_map.cpp
61 lines (54 loc) · 1.58 KB
/
unordered_map.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
#include <unordered_map>
#include <string>
#include <string_view>
#include <iostream>
#include <cctype>
using namespace std;
class Person {
string lastname, firstname;
static string toUpper(string_view);
public:
Person(string_view firstname, string_view lastname) : lastname{ lastname }, firstname{ firstname } {}
friend bool operator==(const Person&, const Person&);
friend ostream& operator<<(ostream&, const Person&);
friend class hash<Person>;
};
bool operator==(const Person& lhs, const Person& rhs) {
return Person::toUpper(lhs.lastname + ',' + lhs.firstname)
== Person::toUpper(rhs.lastname + ',' + rhs.firstname);
}
ostream& operator<<(ostream& os, const Person& p) {
return os << p.firstname << ' ' << p.lastname;
}
string Person::toUpper(string_view s) {
string upper;
for (auto c : s) {
if (isalpha(c)) {
upper.push_back(toupper(c));
}
}
return upper;
}
namespace std {
template <>
struct hash<Person> {
size_t operator()(const Person& p) const {
return hash<string>()(p.firstname + p.lastname);
}
};
}
int main() {
unordered_map<Person,int> Scientists{
{ { "Albert", "Einstein" }, 1879 },
{ { "Marie", "Curie" }, 1867 },
{ { "Isaac", "Newton" }, 1643 },
{ { "Charles", "Darwin" }, 1809 },
{ { "Galileo", "Galilei" }, 1564 },
{ { "Nikola", "Tesla" }, 1856 },
{ { "Stephen", "Hawking" }, 1942 },
{ { "Alan", "Turing" }, 1912 }
};
for (const auto& s : Scientists) {
cout << s.first << " born in " << s.second << '\n';
}
}