Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL + Ordered set + without duplicates

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 ?

like image 648
Spectral Avatar asked Dec 16 '10 16:12

Spectral


People also ask

Can ordered set contain duplicates?

The ordered set keeps all the elements in a sorted order and doesn't allow duplicate values.

Which containers do not allow duplicates?

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.

Is set STL ordered?

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().


1 Answers

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.

like image 98
CadentOrange Avatar answered Sep 25 '22 06:09

CadentOrange