Using a set
A logical extension to our program is to ignore common words like “the,” “and,” “or,” and so on. We’ll use a set to hold the words we want to ignore and count only those words that are not in this set:
// count the number of times each word occurs in the inputmap<string, size_t> word_count; // empty map from string to size_tset<string> exclude = {"The", "But", "And", "Or", "An", "A", "the", "but", "and", "or", "an", "a"};string word;while (cin >> word) // count only words that are not in exclude if (exclude.find(word) == exclude.end()) ++word_count[word]; // fetch and increment the counter for word
Like the other containers, set is a template. To define a set, we specify the type of its elements, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access