c++ unordered_map collision handling , resize and rehash

后端 未结 3 2161
旧时难觅i
旧时难觅i 2021-02-13 04:10

I have not read the C++ standard but this is how I feel that the unordered_map of c++ suppose to work.

  • Allocate a memory block in the heap.
  • With every p
3条回答
  •  猫巷女王i
    2021-02-13 04:43

    With every put request, hash the object and map it to a space in this memory

    Unfortunately, this isn't exactly true. You are referring to an open addressing or closed hashing data structure which is not how unordered_map is specified.

    Every unordered_map implementation stores a linked list to external nodes in the array of buckets. Meaning that inserting an item will always allocate at least once (the new node) if not twice (resizing the array of buckets, then the new node).

    No, that is not at all the most efficient way to implement a hash map for most common uses. Unfortunately, a small "oversight" in the specification of unordered_map all but requires this behavior. The required behavior is that iterators to elements must stay valid when inserting or deleting other elements. Because inserting might cause the bucket array to grow (reallocate), it is not generally possible to have an iterator pointing directly into the bucket array and meet the stability guarantees.

    unordered_map is a better data structure if you are storing expensive-to-copy items as your key or value. Which makes sense, given that its general design was lifted from Boost's pre-move-semantics design.

    Chandler Carruth (Google) mentions this problem in his CppCon '14 talk "Efficiency with Algorithms, Performance with Data Structures".

提交回复
热议问题