-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch10_Sec3_mapFamNameEx.cpp
45 lines (42 loc) · 1.19 KB
/
Ch10_Sec3_mapFamNameEx.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
// C++ Primer 4th Edition Chapter 10 Exercises Section 10.3.9 Exercise 10.18
#include<iostream>
#include<utility>
#include <map>
#include<vector>
#include<string>
using namespace std;
typedef map< string, vector<string> > FamilyMap;
int main()
{
FamilyMap children;
string surname, childName;
do{
cout << "Enter surname (Ctrl+Z to exit):" << endl;
cin >> surname;
if ( !cin )
break;
vector<string> 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 )
ret.first->second.push_back( childName );
cin.clear();
} while ( cin );
cin.clear();
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( vector<string>::iterator iter = ret->second.begin();
iter != ret->second.end(); iter++ )
cout << *iter++ << endl;
}
return 0;
}