题目要求如下
1.自己版本
#pragma once
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
ifstream t_if ("C:\\Users\\16519\\Desktop\\shenqingshu.txt");
string line;
string word;
int line_number = 0;
map<string, set<int>> ss_map;
vector<string> text_s;
while (getline(t_if, line))
{
line_number++;
printf("line_number:%d:%s\n", line_number, line.c_str());
text_s.push_back(line);
istringstream iss(line);
while (iss >> word)
{
if (ss_map[word].find(line_number) == ss_map[word].end())
{
//cout << word << " insert line_number: " << line_number << ";";
ss_map[word].insert(line_number);
}
else
{
}
}
//cout << endl << endl;
}
while (true)
{
printf("please input you want to search number(you can input 'q' to stop):\n");
string search_number;
if (!(cin >> search_number) || search_number == "q") break;
if (ss_map.find(search_number) != ss_map.end())
{
printf("the number you are searching is: %s:\n", search_number.c_str());
set<int> s_temp = ss_map[search_number];
for (auto j : s_temp)
{
cout << j << ":" << text_s[j - 1] << endl;
}
cout << endl;
}
else
{
printf("Error! The number you are searching doesn't exit.\n\n");
}
}
return 0;
}