-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.28.cpp
60 lines (56 loc) · 1.46 KB
/
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
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <fstream>
#include <sstream>
#include <cstddef>
using std::istringstream;
using std::string;
using std::map;
using std::set;
using std::vector;
using std::ifstream;
using std::cout;
using std::endl;
using std::cin;
using std::getline;
using std::size_t;
vector<string> readFile(ifstream &input) {
vector<string> content;
for (string line; getline(input, line); content.push_back(line)) ;
return content;
}
map<string, set<size_t>> mapper(const vector<string> &vec) {
map<string, set<size_t>> ret;
size_t lineNum = 1;
for (const string &line : vec) {
istringstream stream(line);
string word;
while (stream >> word) {
ret[word].insert(lineNum);
}
++lineNum;
}
return ret;
}
int main(int argc, char *argv[]) {
if (argc < 2) return -1;
ifstream infile(argv[1]);
vector<string> text = readFile(infile);
map<string, set<size_t>> word_line = mapper(text);
while (true) {
string s;
cout << "enter word to look for, or q to quit: ";
if (!(cin >> s) || s == "q") break;
if (word_line.find(s) != word_line.end()) {
size_t occurs = word_line.find(s)->second.size();
cout << s << " occurs " << occurs << " times" << endl;
for (set<size_t>::const_iterator lineNumIt = word_line.find(s)->second.cbegin(); lineNumIt != word_line.find(s)->second.cend(); ++lineNumIt) {
cout << " (line " << *lineNumIt << ") " << text[*lineNumIt - 1] << endl;
}
}
}
return 0;
}