\quad
建立5个map,用于书名,作者,关键词,出版社,出版日期与书编号之间的映射,然后输出即可。
Tips:获得一句字符串后,比如“test code debug sort keywords”,如何将其按空格分成若干个单词,借用sstream头文件里面的istringstream可以很方便的完成。
#include <sstream>
string s, temp;
vector<string> v;
istringstream is(s); // 进入字符串流进行重新读入
while(is >> temp) v.push_back(id); // 这样,v里面存放的就是按空格分割好的各个单词
\quad 程序如下所示
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <sstream>
using namespace std;
map<string, vector<string> > mp[6];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int N; cin >> N;
string id, s, temp;
getline(cin, s, '\n');
while(N--)
{
getline(cin, id, '\n');
for (int i = 1; i <= 5; ++i)
{
getline(cin, s, '\n');
if(i!=3) mp[i][s].push_back(id);
else{
istringstream is(s); // 进入字符串流进行重新读入
while(is >> temp) mp[3][temp].push_back(id);
}
}
}
int M; cin >> M;
getline(cin, s, '\n');
while(M--)
{
getline(cin, s, '\n');
int flag = s[0]-'0';
cout << s << endl;
string key = s.substr(3);
if(mp[flag].count(key)>0){
vector<string> res = mp[flag][key];
sort(res.begin(), res.end());
for (int i = 0; i < res.size(); ++i){
cout << res[i] << endl;
}
}
else cout << "Not Found" << endl;
}
return 0;
}