23.4 Sets - Dictionary Example

Program to check for misspelled words. A dictionary is read into a set, then checks input words against the set.

void spellCheck(istream& dictionary, istream& text)
{
   set<string> words;
   string word;

   // First put all words from dictionary into set.
   while (dictionary >> word)
      words.insert(word);

   // Then read words from text
   while (text >> word)
      if (words.count(word) == 0)
         cout << "Misspelled word " << word << "\n";
}

prev |top |next