注意同一行有很多的keyword所以输入要判断是否到达了最后一个输入,也即是否到达了换行符的位置;
while(1)
{
cin>>keyword;
mp[keyword].insert(temp);
char c=getchar();
if(c=='\n')
break;
}
本题在最后的输出过程应使用*it语句,实际上这里已经转换成了解决set的输出问题。
#include<iostream>
#include<map>
#include<set>
#include<cstdio>
#include<string>
using namespace std;
map<string , set<int>> mp;
int main()
{
int n,m,temp,k;
string title,author,keyword, publisher,year,str;
cin>>n;
for (int i=0;i<n;i++)
{
scanf("%d",&temp);
getchar();
getline(cin,title);
getline(cin,author);
while(1)
{
cin>>keyword;
mp[keyword].insert(temp);
char c=getchar();
if(c=='\n')
break;
}
getline(cin,publisher);
getline(cin,year);
mp[title].insert(temp);
mp[author].insert(temp);
mp[publisher].insert(temp);
mp[year].insert(temp);
}
cin>>m;
for (int i=1;i<=m;i++)
{
scanf("%d: ",&k);
getline(cin,str);
printf("%d: ",k);
cout<<str<<endl;
if(mp.find(str)==mp.end())
printf("Not Found\n");
else
{
for(auto it=mp[str].begin();it!=mp[str].end();it++)
{
printf("%07d\n",*it);
}
}
}
}
本文介绍了一个基于C++的图书信息检索系统实现,利用标准输入流读取图书标题、作者、关键词等信息,并将其存储在map和set数据结构中。系统支持按关键词搜索图书,返回所有匹配的图书ID。
716

被折叠的 条评论
为什么被折叠?



