forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex11_23.cpp
57 lines (45 loc) · 1.56 KB
/
ex11_23.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
//! @Alan
//!
//! Exercise 11.23:
//! Rewrite the map that stored vectors of children’s names with a key that is
//! the family last name for the exercises in § 11.2.1 (p. 424) to use a multimap.
// note that multimap has no [] operator defined.
//!
//! 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.
// Discussion on Stack Overflow:
// http://stackoverflow.com/questions/20608365/is-it-possible-to-code-this-waywhilelambda
//!
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
int main()
{
std::multimap<std::string, std::vector<std::string>> faml;
std::string lastName, childName;
std::vector<std::string> v_chidrenName;
//! read the last name
while(std::cin >> lastName && lastName != "@q")
{
std::cout << "please input all children's names.Enter @q when input is done\n";
//! read the children's names
while(std::cin >> childName && childName != "@q")
v_chidrenName.push_back(childName);
//! using {} to create a pair and inert it into the multimap
faml.insert({lastName,v_chidrenName});
std::cout << "enter another last name:\n";
}
//! print the content of the multimap
for(const auto &e : faml)
{
std::cout << e.first << ":\n";
for(const auto &l : e.second)
std::cout << l << " ";
std::cout << "\n";
}
return 0;
}