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!
This is a discussion on container question within the C++ Programming forums, part of the General Programming Boards category; Hello Is there any c++ container that will only allow/keep unique elements. I need a vector of strings, which should ...
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.
I used to be an adventurer like you... then I took an arrow to the knee.
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.