Can someone show me an example?

Write code that parses words out of the string s (Here’s one method: Split a string in C++?). Print the words as you parse them and then put them in some sort of set container. A set container does not allow duplicates. Once done parsing words, iterate over the set and print the words out (there won’t be duplicates).

If you need to print out the de-duped words in the order they appeared in the string, then you could keep a vector alongside the set. Like before, as you parse the words add them to the set but inspect the returned pair from the set’s insert() method (it can tell you whether the inserted item was new to the set or if the insert operation was denied because the set already contained a value that was equal to the one you tried to insert: http://www.cplusplus.com/reference/set/set/insert/).

std:vector<std:string> wordVector; // Assume populated with raw parsed words
std:set<std:string> deDupedSet;
std:vector<std:string> deDupedVector;

for (int i = 0; i < wordVector.size(); i++)
{
    if (deDupedSet.insert(wordVector[i]).second())
    {
        deDupedVector.push_back(wordVector[i]);
    {
}
// Print the deDupedVector afterwards

Leave a Comment