forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex11_14.cpp
66 lines (57 loc) · 1.65 KB
/
ex11_14.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
62
63
64
65
66
//! @Yue Wang
//! Exercise 11.14:
//! Extend the map of children to their family name that you wrote for the
//! exercises in § 11.2.1 (p. 424) by having the vector store a pair that
//! holds a child’s name and birthday.
//!
//! Exercise 11.7:
//! Define a map for which the key is the family’s last name and
//! the value is a vector of the children’s names. Write code to
//! add new families and to add new children to an existing family.
//!
#include <iostream>
#include <map>
#include <string>
#include <vector>
using std::ostream;
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::make_pair;
using std::pair;
using std::vector;
using std::map;
class Families {
public:
using Child = pair<string, string>;
using Children = vector<Child>;
using Data = map<string, Children>;
void add(string const& last_name, string const& first_name, string birthday)
{
_data[last_name].push_back(make_pair(first_name, birthday));
}
ostream& print(std::ostream& os) const
{
if (_data.empty()) return os << "No data right now." << endl;
for (const auto& pair : _data) {
os << pair.first << ":\n";
for (const auto& child : pair.second)
os << child.first << " " << child.second << endl;
os << endl;
}
return os;
}
private:
Data _data;
};
int main()
{
Families families;
string message = "Please enter last name, first name and birthday";
for (string l, f, b; cout << message << endl, cin >> l >> f >> b;
families.add(l, f, b))
;
families.print(cout << "Current data:" << endl);
return 0;
}