CJCoding With Joseph
15free / daySign inCreate account

Unique Word Tracker

Build a class UniqueWords that stores the distinct words in a set<string>. It must provide these public methods:

1. void add(const string& word): records the word. Every call counts toward the total, but a word that was added before does not increase the unique count.
2. int unique() const: returns how many distinct words have been added.
3. int totalAdded() const: returns how many times add has been called in total, including repeats.
4. bool seen(const string& word) const: returns true if the word has been added at least once, false otherwise.
5. void printSorted() const: prints each distinct word on its own line in alphabetical order. Prints nothing at all if no words were added.

The test prints the labels itself. After adding apple, pear, apple, kiwi, pear (in that order) (see Expected Output).

Do NOT write a main function.

Expected Output:

Unique: 3
Total: 5
apple
kiwi
pear
Unique: 0
Total: 0
true
false
zebra

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
STL ContainersSets
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.