This repository was archived by the owner on Aug 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_Names1.cpp
More file actions
97 lines (87 loc) · 2.01 KB
/
04_Names1.cpp
File metadata and controls
97 lines (87 loc) · 2.01 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <map>
#include <algorithm>
#include <sstream>
using namespace std;
class Person {
public:
void ChangeFirstName(int year, const string& first_name) {
if (m.find(year) == m.end()) {
m[year]["first"] = first_name;
m[year]["last"] = "no";
} else {
m[year]["first"] = first_name;
}
}
void ChangeLastName(int year, const string& last_name) {
if (m.find(year) == m.end()) {
m[year]["first"] = "no";
m[year]["last"] = last_name;
} else {
m[year]["last"] = last_name;
}
}
string GetFullName(int year) {
if (m.empty() || m.begin()->first > year) {
return "Incognito";
}
auto it = m.begin();
if (m.find(year) != m.end()) {
it = m.find(year);
} else {
it = m.end();
it--;
while (it->first > year && it != m.begin()) {
it--;
}
}
stringstream ss;
string first = getFirst(it);
string last = getLast(it);
if (first == "no") {
ss << last << " with unknown first name";
} else if (last == "no") {
ss << first << " with unknown last name";
} else {
ss << first << " " << last;
}
return ss.str();
}
private:
map<int, map<string, string> > m;
template<typename T>
string const &getFirst(T it) {
if (it->second["first"] != "no") {
return it->second["first"];
}
while (it->second["first"] == "no" && it != m.begin())
it--;
return it->second["first"];
}
template<typename T>
string const &getLast(T it) {
if (it->second["last"] != "no") {
return it->second["last"];
}
while (it->second["last"] == "no" && it != m.begin())
it--;
return it->second["last"];
}
};
int main() {
Person person;
person.ChangeFirstName(1965, "Polina");
person.ChangeLastName(1967, "Sergeeva");
for (int year : {1900, 1965, 1990}) {
cout << person.GetFullName(year) << endl;
}
person.ChangeFirstName(1970, "Appolinaria");
for (int year : {1969, 1970}) {
cout << person.GetFullName(year) << endl;
}
person.ChangeLastName(1968, "Volkova");
for (int year : {1969, 1970}) {
cout << person.GetFullName(year) << endl;
}
return 0;
}