How to remove same words from set?

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
unsigned long 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];
           }
           else if(!words && word != ""){
               uw.insert(word);
               word = "";
           }
   }
   if (word != "")
       uw.insert(word);
   return uw.size();
}
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.
std::set only stores unique values.
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?
Add each word to the set and then display the size of the set?
I would change your function's signature to

unsigned long countUWords(const string& s, set<string>& uw)

to pass it the set uw, instead of declaring it in the function.
If I move it there, I get error:
error: too few arguments to function 'long unsigned int countUWords(const string&, std::set<std::basic_string<char> >&)'


What do I have to do to fix it? Sorry, I'm still pretty new to C++. Thanks for helping out though, really appreciate it.
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.
Ah, yes. I included that but it's still giving me wrong answers, larger numbers in fact. Maybe I need to change what is to be cout?

Edit: Yep, I had to change what was in my cout. Thanks for the help!!!
Last edited on
Topic archived. No new replies allowed.