forked from demon90s/CppStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_12_28.cpp
73 lines (60 loc) · 1.31 KB
/
exercise_12_28.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 练习12.28:编写程序实现文本查询,不要定义类来管理数据。你的程序应该接受一个
// 文件,并与用户交互来查询单词。使用vector、map和set容器来保存来自文件的数据并
// 生成查询结果
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
// 读入文件
ifstream infile("../data/some_words.txt");
if (!infile)
{
cout << "Cant open file" << endl;
return 1;
}
vector<string> lines;
map<string, set<int>> word_to_line_map;
// 存储数据
{
string line;
size_t line_no = 0;
while (getline(infile, line))
{
lines.push_back(line);
istringstream iss(line);
string word;
while (iss >> word)
{
word_to_line_map[word].insert(line_no);
}
++line_no;
}
}
// 与用户交互
do
{
cout << "Enter word to look for, or q to quit: ";
string s;
if (!(cin >> s) || s == "q") break;
// 查询
if (word_to_line_map.find(s) != word_to_line_map.end())
{
cout << s << " occurs " << word_to_line_map[s].size() << " times" << endl;
for (auto l : word_to_line_map[s])
{
cout << "(line " << l << ") " << lines[l] << endl;
}
}
else
{
cout << "Cant find " << s << endl;
}
} while (true);
return 0;
}