-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch10_Sec5_multimapEx.cpp
75 lines (67 loc) · 2.21 KB
/
Ch10_Sec5_multimapEx.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
67
68
69
70
71
72
73
74
75
// C++ Primer 4th Edition Chapter 10 Exercises Section 10.5.2 Exercise 10.26 & 10.27 & 10.28
#include<iostream>
#include<map>
#include<string>
using namespace std;
typedef multimap<string, string> Author;
typedef Author::iterator AuIter;
int main()
{
Author authors;
string authorName, authorWork, searchItem;
// Preparation Work : cin some items
do{
cout << "Enter author name(Ctrl+Z to quit):" << endl;
cin >> authorName;
if ( ! cin )
break;
cout << "Enter author's work (Ctrl+Z to quit)" << endl;
while ( cin >> authorWork )
authors.insert( make_pair(authorName,authorWork) );
cin.clear();
} while ( cin );
cin.clear();
// Search and Erasure work
cout << "Who is the author that you want to erase? :" << endl;
cin >> searchItem;
Author::iterator iter;
// for Exercise 10.26
iter = authors.find( searchItem );
if ( iter != authors.end() )
authors.erase( searchItem );
else
cout << "No match found!" << endl;
// end for Exercise 10.26
// for Exercise 10.27
pair<AuIter, AuIter> pos = authors.equal_range( searchItem );
if ( pos.first != pos.second )
authors.erase( pos.first, pos.second );
else
cout << "No match found!" << endl;
// end for Exercise 10.27
// Final stored items Output
cout << "author\t\twork:" << endl;
for( iter = authors.begin(); iter != authors.end(); iter++)
cout << iter->first << "\t\t" << iter->second << endl;
// for Exercise 10.28
iter = authors.begin();
if ( iter == authors.end() )
{
cout << "Empty multimap!" << endl;
return 0;
}
string currAuthor, preAuthor;
do{
currAuthor = iter->first;
if ( preAuthor.empty() || currAuthor[0] != preAuthor[0] )
cout << "Author Names Begining with '" << currAuthor[0] << "' :" <<endl;
cout << currAuthor;
for (pos = authors.equal_range(currAuthor); pos.first != pos.second; pos.first++)
cout << ", " << pos.first->second;
cout << endl;
iter = pos.second;
preAuthor = currAuthor;
} while ( iter != authors.end() );
// end for Exercise 10.28
return 0;
}