The fact that the following member functions are offered by std::unordered_map
suggests that it is based on a hashed-table, perhaps
separate chaining with linked lists.
bucket_count, hash_function, load_factor, max_load_count, rehash
Whether the elements are contiguous or not depends on the allocator. The
default allocator for the unordered_map
and list
does not allocate the
elements in contiguous memory. The memory for each element is allocated
at the time of its insertion.
However, you can provide a custom allocator (such as a pool allocator)
which may allocate the elements from a pre-allocated memory pool. Still,
the logically adjacent elements in the data structure may not be physically
adjacent in the memory.
So, if looping through all the elements is the most frequent operation, then
the unordered_map
may not be best solution. Running the dominant use cases through a profiler for all competing solutions would reveal the best solution.
In addition to that, unordered_map
is not the best choice to loop for another
reason. Note the word "unordered" in the name, it conveys that -- unlike list
, vector
, or map
-- there is no order of the elements. For example, the member
function rehash
may change the relative order of the elements. In fact,
rehashes are automatically performed by the container whenever its load factor
is going to exceed the max_load_factor
during any operation.
unordered_set
vs.vector
in your scenario. Try to use smart pointers. Maybeunique_ptr
, you will have to write pure evil code to get duplicates of those ;) – Profligatestd::unordered_set<>
away from answering this yourself, and once cracked open you'll find no chance of a contiguous implementation (and what would it matter if it was? the most expensive part of a decent hash-table implementation is the hash function itself (degenerate linked-list via unbounded collisions not withstanding)). – Azedarachunordered_set
implementation will use 'empty' hash function for the pointer type. Also this is very easy to implement. – Expertunordered_set
implementation will use whatever hash function the user tells it to use, empty or not! – Deventerunique_ptr
orshared_ptr
? If you aren't I would really suggest using smartpointers. – Natalianatalie