forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.7.cpp
44 lines (37 loc) · 1.28 KB
/
11.7.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
#include <string>
#include <map>
#include <vector>
#include <iostream>
typedef std::map<std::string, std::vector<std::string>> family_type;
void addFamily(family_type &families, const std::string &name) {
auto ret = families.insert({name, std::vector<std::string>()});
if (!ret.second)
std::cerr << "Error: Already has family <" << name << ">" << std::endl;
}
void addChild(family_type &families, const std::string &family_name,
const std::string &child_name) {
auto it = families.find(family_name);
if (it != families.end())
it->second.push_back(child_name);
else
std::cerr << "Error: No family <" << family_name << "> for child <"
<< child_name << ">" << std::endl;
}
int main() {
family_type families;
addFamily(families, "Zhang");
addFamily(families, "Li");
addFamily(families, "Wang");
addFamily(families, "Zhang"); // Error
addChild(families, "Zhang", "San");
addChild(families, "Zhang", "Bao");
addChild(families, "Zhang", "Tian");
addChild(families, "Li", "Si");
addChild(families, "Wang", "Wu");
addChild(families, "Wang", "San");
addChild(families, "Zhao", "Liu"); // Error
for (const auto &family : families)
for (const auto &name : family.second)
std::cout << family.first << " " << name << std::endl;
return 0;
}