-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.09.cpp
45 lines (42 loc) · 900 Bytes
/
11.09.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
#include <map>
#include <list>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
using std::string;
using std::cout;
using std::endl;
using std::ifstream;
using std::map;
using std::list;
using std::istringstream;
std::ostream &print(std::ostream &os, const map<string, list<unsigned>> &m) {
for (const auto &e : m) {
os << e.first << ": ";
for (unsigned u : e.second) {
os << u << ' ';
}
os << endl;
}
return os;
}
int main(int argc, char *argv[]) {
string filename = argv[1];
cout << filename << endl;
ifstream infile(filename);
map<string, list<unsigned>> word_line;
string line;
unsigned lineNum = 1;
while (getline(infile, line)) {
istringstream stream(line);
string word;
while (stream >> word) {
list<unsigned> &lineCountL = word_line[word];
lineCountL.push_back(lineNum);
}
++lineNum;
}
print(cout, word_line);
return 0;
}