Hello
Is there any c++ container that will only allow/keep unique elements.
I need a vector of strings, which should be all unique (no duplicates).
What would be the best way to solve this?
Many thanks in advance!
Hello
Is there any c++ container that will only allow/keep unique elements.
I need a vector of strings, which should be all unique (no duplicates).
What would be the best way to solve this?
Many thanks in advance!
The set container would only allow unique values to be stored, a set<string> container for example would not allow two copies of the word "apple" to be inserted however things are case sensitive so "apple" and "Apple" would be considered different and therefore both would be allowed into the container.
That is as a default of course (which uses the default operator< as a comparison object), you could use the optional second parameter to the instantiation of the set container such that it uses your own custom comparison function that would (for instance) disregard case when comparing objects and therefore not allow "Apple" if "apple" had already existed within the container.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
The set is probably your best bet, but depending on your situation you might also just add all your data to a vector, then use algorithms to remove non-unique elements. This might make sense if you don't have many duplicates and you add all your data to the container once at the beginning before you need duplicates removed.