-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.cpp
55 lines (49 loc) · 1.46 KB
/
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
#include <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&);
};
bool operator<(const Person& lhs, const Person& rhs) {
if (lhs.lastname != rhs.lastname) {
return Person::toUpper(lhs.lastname) < Person::toUpper(rhs.lastname);
}
else {
return Person::toUpper(lhs.firstname) < Person::toUpper(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;
}
int main() {
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';
}
}