可能的查询 ID值进行map映射
以下代码有问题,原因是ID是整形保存,会忽略前面的0
改为字符串保存ID
以下代码有问题,原因是ID是整形保存,会忽略前面的0
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
int main()
{
int N;
map<string,set<int>> m;
int ID;
int index;
string title;
string author;
string keyword;
string publisher;
string year;
int M;
string query;
cin>>N;
while(N--)
{
cin>>ID;
getchar();
getline(cin,title);
getline(cin,author);
getline(cin,keyword);
index=1;
while(index!=string::npos)
{
index = keyword.find(" ");
m["3: "+keyword.substr(0,index)].insert(ID);
//cout<<keyword.substr(0,index)<<endl;
keyword = keyword.substr(index+1);
}
getline(cin,publisher);
getline(cin,year);
m["1: "+title].insert(ID);
m["2: "+author].insert(ID);
m["4: "+publisher].insert(ID);
m["5: "+year].insert(ID);
}
cin>>M;
getchar();
while(M--)
{
getline(cin,query);
cout<<query<<endl;
if(m.find(query)!=m.end())
{
for(set<int>::iterator si= m[query].begin(); si != m[query].end();si++)
cout<<*si<<endl;
}
else
{
cout<<"Not Found"<<endl;
}
}
}
改为字符串保存ID
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
int main()
{
int N;
map<string,set<string>> m;
string ID;
int index;
string title;
string author;
string keyword;
string publisher;
string year;
int M;
string query;
cin>>N;
getchar();
while(N--)
{
getline(cin,ID);
getline(cin,title);
getline(cin,author);
getline(cin,keyword);
index=1;
while(index!=string::npos)
{
index = keyword.find(" ");
m["3: "+keyword.substr(0,index)].insert(ID);
//cout<<keyword.substr(0,index)<<endl;
keyword = keyword.substr(index+1);
}
getline(cin,publisher);
getline(cin,year);
m["1: "+title].insert(ID);
m["2: "+author].insert(ID);
m["4: "+publisher].insert(ID);
m["5: "+year].insert(ID);
}
cin>>M;
getchar();
while(M--)
{
getline(cin,query);
cout<<query<<endl;
if(m.find(query)!=m.end())
{
for(set<string>::iterator si= m[query].begin(); si != m[query].end();si++)
cout<<*si<<endl;
}
else
{
cout<<"Not Found"<<endl;
}
}
}