I need to have an ordered set of values without duplicates. So, what is the fast/best method :
1 - Create a vector, sort it and remove duplicates ? 2 - Use a kind of "sorted" vector (if it exists) ?
Which one can be the more efficient ?
The ordered set keeps all the elements in a sorted order and doesn't allow duplicate values.
So, the conclusion, use std::unordered_set or std::unordered_map (if you need the key-value feature). And you don't need to check before doing the insertion, these are unique-key containers, they don't allow duplicates.
Since set is ordered, we can use functions like binary_search(), lower_bound() and upper_bound() on set elements. These functions cannot be used on unordered_set().
Use std::set. It's ordered, and it does not allow duplicates.
The only downside is that you don't get random access to the elements though this was not specified as a requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With