10.28

#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;
}

转载于:https://my.oschina.net/ppppower/blog/37011

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值