EDIT: So I started with removing stopwords form a string, but rather than editing the string itself, I take each word form the string and sepatate them into a vector<string>;
-------------
My project is to create a function that recieves a string(by reference), then removes the stop words from that string.
I have the stop words sorted in the file stopwords.txt
e.g.
1 2 3 4
|
a
about
above
across
|
The first part of my function takes each stop word and puts it in vector<string>stopwords. This part works fine.
The second part should remove each instance of the stop word from the string, but an error occurs in #include<algorithm>
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
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void filterStopWords(string &str) {
string lineread;
ifstream sw("stopwords.txt");
vector<string>stopwords;
while (sw.good()) {
getline(sw, lineread);
stopwords.push_back(lineread);
}
sw.close();
string::iterator itr, itr2; // two iterators to select stop words
for (unsigned int i=0; i<stopwords.size(); i++) { // for every stopword in vector<string>stopwords...
int reps = Count(stopwords.at(i), str); // count number of occurences of stop word
for (unsigned int j=0;j<reps; j++) { // for every time stopword.at(i) occurs...
itr = find(str.begin(),str.end(),stopwords.at(i)); // first iterator set to position of stop word
itr2 = stopwords.at(i).end(); // second iterator set to the size of stop word
str.erase(itr, itr2); // erase specific instance of stop word
}
}
}
|
the Count() function shouldn't be the problem, but I'll post it just in case...
1 2 3 4 5 6 7 8 9 10
|
int Count(const string & str, const string & obj ) //function counts how many instances of a substring are in a string
{
int n = 0;
string::size_type pos = 0;
while((pos = obj.find(str, pos)) != string::npos ) {
n++;
pos += str.size();
}
return n;
}
|
P.S. the string that is passed to my function is about a megabyte is size, so efficiency is somewhat important