I want to find the number of unique words. I have a code that does that but not quite what I want. It is only processing one line at a time and not looking at all of the lines at the same time to find the unique words, thus giving me the incorrect number of unique words.
So, I was thinking whether or not if there was a way to remove the same word that has already been inserted into the set. I am not sure how to go about it or if there's another way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
unsignedlong countUWords(const string& s)
{
set<string> uw;
string word = "";
for(size_t i = 0; i < s.size(); i++){
bool words = (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z');
if(words){
word += s[i];
}
elseif(!words && word != ""){
uw.insert(word);
word = "";
}
}
if (word != "")
uw.insert(word);
return uw.size();
}
Okay that is true. Let me ask a different question then. How do I let it check all the lines from any text first then count the number of unique words?
Did you edit your function call appropriately so that you give it the set as the second parameter? The error message suggests you didn't edit where you called the function.