Map Help

I have program that is supposed to read in a story from an input file and separate the words and output the lines on which the word occurs. It needs to read in another input file that has a list of words (1 per line) to ignore. i.e. skip them when running through the story. This is what I have so far, I've changed multiple things trying to get it running, and I think I hurt myself more than help.

#include<iostream>
#include<fstream>
#include<map>
#include<set>
#include<vector>
#include<string>
#include"split.h"


using std::cout;
using std::vector;
using std::string;
using std::map;
using std::set;
int main(int argc, char** argv){

cout << "Written by: Chris Dunning \n"<<"on: " << __DATE__ << '\n'<<"at " << __TIME__ << '\n';
cout<<'\n';
map<string,set<unsigned int>> m;
vector<string> words;
string line1, line2;
int index = 0;
set<string> ignore;
string delims = " ,.?!@#$%^&*()_+1234567890-=|'<>";
std::ifstream in1, in2;
std::map<string, set<unsigned int>>::iterator mit;

in1.open("Alice.txt");
in2.open("ignore.txt");
while(!in2.eof()){
getline(in2, line2);
ignore.insert(line2);
}
while(!in1.eof()){
getline(in1, line1);
words=split(line1, delims);
for(unsigned int i = 0; i<words.size(); ++i){
if(ignore.find()==words.end()){
mit = m.find(words[i]);
m(words).insert(index);
if(mit == m.end())
m.insert(mit, make_pair<string,set<unsigned int>>(words[i], index));
else
(*mit).second.insert(index);
}
}
}
for(mit = m.begin(); mit!=m.end(); ++mit){
cout << (*mit).first << " " << (*mit).second << '\n';
}
}
When reading the file, you correctly read the line with getline. But if you make an in-memory stream from the line you read, you can parse it with >>. That way you skip all that white space processing. For example:
1
2
3
4
5
6
7
8
9
10
11
std::ifstream in("Alice.txt");
std::string line;
while (std::getline(in, line))
{
    std::istringstream ins(line);
    std::string word;
    while (ins >> word)
        std::cout << word << std::endl;

    std::cout << std::endl;
}


I have program that is supposed to read in a story from an input file and separate the words and output the lines on which the word occurs.
So you need to read the line, keep track of the line number and store each work in a disctionary against that line number ... and do that for each line. So will have duplicate words, so you need a multimap rather than a map.
Last edited on
Topic archived. No new replies allowed.