#include<iostream>
#include<cctype>
#include<map>
#include<string>
#include<utility>
using namespace std;
int main()
{
//输入作者及作者的作品
multimap<string,string> authorMap;
string author,bookName;
do
{
cout<<"***Enter author's name(Ctrl+d to end):"<<endl;
cin>>author;
if(!cin)
break;
cout<<"***Enter author's work(Ctrl+d to end):"<<endl;
while(cin>>bookName)
authorMap.insert(make_pair(author,bookName));
cin.clear();
}while(cin);
cin.clear();
//判断输入是否成功
if(authorMap.empty())
{
cout<<"***empty multimap!"<<endl;
return 0;
}
//删除某一作者及其作品
cout<<"***Enter the search item(author),and delete(Ctrl+d to end):"<<endl;
string searchItem;
while(cin>>searchItem)
{
typedef multimap<string,string>::iterator iter;
pair<iter,iter> pos=authorMap.equal_range(searchItem);
if(pos.first==pos.second)
{
cout<<"***we do not have this author(key),enter again!"<<endl;
continue;
}
authorMap.erase(pos.first,pos.second); //删除一段范围内的元素
//输出剩余的元素
cout<<endl<<"the rest elements are:"<<endl;
iter it=authorMap.begin();
while(it!=authorMap.end())
{
cout<<"Author Names Beginning with '"<<it->first[0]<<"':"<<endl;
cout<<it->first;
pair<iter,iter> pos=authorMap.equal_range(it->first);
while(pos.first!=pos.second)
{
cout<<", "<<pos.first->second;
++pos.first;
}
cout<<endl;
it=pos.second; //指向下一个键的位置
}
cout<<"***Enter the search item(author), and delete(Ctrl+d to end):"<<endl;
}
return 0;
}