Unordered sets are containers that store unique elements in no particular order, and which allow for fast retrieval of individual elements based on their value.
In an unordered_set, the value of an element is at the same time its key, that identifies it uniquely. Keys are immutable, therefore, the elements in an unordered_set cannot be modified once in the container – they can be inserted and removed, though. Internally, the elements in the unordered_set are not sorted in any particular order.
set vs undered_set
Set set is an ordered sequence of unique keys whereas unordered_set is a set in which key can be stored in any order, so unordered.
Set is implemented as balanced tree structure that is why it is possible to maintain an order between the elements (by specific tree traversal). Time complexity of set operations is O(Log n) while for unordered_set, it is O(1).
Member functions
- std::unordered_set::begin
Returns an iterator pointing to the first element in the unordered_set container or in one of its buckets. An unordered_set object makes no guarantees on which specific element is considered its first element. But, in any case, the range that goes from its begin to its end covers all the elements in the container (or the bucket), until invalidated. - std::unordered_set::end
Returns an iterator pointing to the past-the-end element in the unordered_set container or in one of its buckets. The iterator returned by end does not point to any element, but to the position that follows the last element in the unordered_set container (its past-the-end position). Thus, the value returned shall not be dereferenced. - std::unordered_set::find
Searches the container for an element and returns an iterator to it if found, otherwise it returns an iterator to unordered_set::end (the element past the end of the container).