-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch10_Sec3_mapFamNameBirthEx.cpp
46 lines (44 loc) · 1.33 KB
/
Ch10_Sec3_mapFamNameBirthEx.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
// C++ Primer 4th Edition Chapter 10 Exercises Section 10.3.9 Exercise 10.19
#include<iostream>
#include<utility>
#include <map>
#include<vector>
#include<string>
using namespace std;
typedef vector< pair<string,string> > PersonMap;
typedef map< string, PersonMap > FamilyMap;
int main()
{
FamilyMap children;
string surname, childName, birthDay;
do{
cout << "Enter surname (Ctrl+Z to exit):" << endl;
cin >> surname;
if ( !cin )
break;
PersonMap chd;
pair<FamilyMap::iterator, bool> ret = children.insert(make_pair(surname,chd));
if ( !ret.second ) {
cout << "repeated surname:" << surname <<endl;
continue;
}
cout << "Enter children's name: (Ctrl+Z to exit):" << endl;
while( cin >> childName >> birthDay )
ret.first->second.push_back( make_pair(childName, birthDay) );
cin.clear();
} while ( cin );
cin.clear();
// Search a surname
cout << "Enter a surname to search:" << endl;
cin >> surname;
FamilyMap::iterator ret = children.find( surname );
if ( ret == children.end() )
cout << "No matched surname " << surname << " found" << endl;
else {
cout << "Children have this " << surname << " surname are:" << endl;
for( PersonMap::iterator iter = ret->second.begin();
iter != ret->second.end(); iter++ )
cout << "Name: "<< iter->first <<"\t\tBithday:" << iter->second << endl;
}
return 0;
}