从文件中读取作者和书籍信息,存入multimap,使用find查找指定作者并删除

本文介绍了一个使用C++实现的简单程序,该程序利用标准模板库中的multimap来存储并管理作者与书籍的关系。具体操作包括从文件读取数据、展示存储的信息、删除指定作者的所有书籍记录,并再次展示更新后的信息。
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	ifstream input("inputfile.txt");
	string author,bookname;
	multimap<string,string> bookInfo;
	//将文件中信息读入multimap
	while (input>>author>>bookname) {
		bookInfo.insert(make_pair(author, bookname));
	}
	input.clear();
	input.close();

	//显示信息
	cout<<"****************************************"<<endl;
	cout<<"The author and the book table "<<endl;
	multimap<string,string>::const_iterator beginIter = bookInfo.begin();
	multimap<string,string>::const_iterator endIter = bookInfo.end();
	for ( ; beginIter != endIter; ++beginIter) {
		cout<<"The name :"<<beginIter->first<<"\t\t"<<"The book :"<<beginIter->second<<endl;
	}
	cout<<"****************************************"<<endl;

	//删除指定作者的相关信息
	string findAuthor;
	cout<<"Please input the delete author : ";
	while (cin >> findAuthor) {
		int delNum = 0;
		multimap<string,string>::const_iterator mulIter = bookInfo.find(findAuthor);
		if (mulIter != bookInfo.end()) {
			delNum = bookInfo.erase(mulIter->first);
		}
		cout<<"The delete number is "<<delNum<<endl;

		//打印删除后信息
		cout<<"****************************************"<<endl;
		cout<<"After delete,the author and the book table "<<endl;
		multimap<string,string>::const_iterator beginIter = bookInfo.begin();
		multimap<string,string>::const_iterator endIter = bookInfo.end();
		for (; beginIter != endIter; ++beginIter) {
			cout<<"The name :"<<beginIter->first<<"\t\t"<<"The book :"<<beginIter->second<<endl;
		}
		cout<<"****************************************"<<endl;
		cout<<"Please input the delete author : ";
	}
	return 0;
}

 

 

保存信息的文件inputfile.txt

'em them
cuz because
gratz grateful
i I
nah no
pos supposed
sez said
tanx thanks
wuz was
nah no
pos supposed
sez said
tanx thanks
wuz was
nah no
pos supposed
sez said
tanx thanks
wuz was

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值