forked from jieniyimiao/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex11_14.cpp
66 lines (55 loc) · 1.77 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
//! @Alan
//! 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.
//!
//! @Alan
//!
//! 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 <algorithm>
int main()
{
//! define a map as required.
std::map<std::string, std::vector<std::pair<std::string,std::string>>>
famlies_map;
//! declare three strings to store the input
std::string lastName, childName, birthday;
while([&](){//! a lambda to read lastName and check if should quit
std::cout << "last name:\n";
std::cin >> lastName;
return lastName != "@q";
}())
{
while([&](){//! a lambda to read child name and birthday and check if should quit
std::cout << "child's name:\n";
std::cin >> childName;
std::cout << "his birthday:\n";
std::cin >> birthday;
return childName != "@q" && birthday != "@q";
}())
{
famlies_map[lastName].push_back({childName, birthday});
//! ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^
//! use lastName as the key create a pair using {}.
}
}
//! print the content.
for(const auto &e : famlies_map)
{
std::cout << e.first <<":\n";
for (const auto &l : e.second)
{
std::cout << l.first << " "
<< l.second << " ";
}
}
return 0;
}