-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab_StringStreamsProcessing.cpp
64 lines (61 loc) · 1.56 KB
/
Lab_StringStreamsProcessing.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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
#include <algorithm>
using namespace std;
map<string,vector<int> >f(istream& a,const vector<string>& b)
{
map<string,vector<int> >out;
int i = 1;
string line;
while(getline(a,line)){
istringstream ss(line);
string word;
while(ss>>word){
if(find(b.begin(),b.end(),word)!=b.end())out[word].push_back(i);
}
i++;
}
return out;
}
int main(int argc, char*argv[])
{
/*ofstream file;
file.open("123");
file.close();*/
char* keywords=argv[1];
char* text2search=argv[2];
char* result=argv[3];
ifstream input_file1;
ifstream input_file2;
ofstream output_file;
input_file1.open(keywords);
input_file2.open(text2search);
output_file.open(result);
string a;
vector<string>key;//存放keywords中的关键词
while(input_file1>>a){
key.push_back(a);
}
input_file1.close();
map<string,vector<int> >out = f(input_file2,key);
input_file2.close();
for(int i=0;i<key.size();i++)
{
output_file << key[i] << " : { ";
if(out[key[i]].size()!=0) {
for (int j = 0; j < out[key[i]].size() - 1; j++) {
output_file << out[key[i]][j] << ",";
}
output_file << out[key[i]][out[key[i]].size()-1];
}
output_file<<" }"<<"\n";
}
output_file.close();
return 0;
}