Is the unordered_map really unordered?

前端 未结 5 2024
悲哀的现实
悲哀的现实 2020-12-06 06:07

I am very confused by the name \'unordered_map\'. The name suggests that the keys are not ordered at all. But I always thought they are ordered by their hash value. Or is th

5条回答
  •  春和景丽
    2020-12-06 06:14

    You are right, unordered_map is actually hash ordered. Note that most current implementations (pre TR1) call it hash_map.

    The IBM C/C++ compiler documentation remarks that if you have an optimal hash function, the number of operations performed during lookup, insertion, and removal of an arbitrary element does not depend on the number of elements in the sequence, so this mean that the order is not so unordered...

    Now, what does it mean that it is hash ordered? As an hash should be unpredictable, by definition you can't take any assumption about the order of the elements in the map. This is the reason why it has been renamed in TR1: the old name suggested an order. Now we know that an order is actually used, but you can disregard it as it is unpredictable.

提交回复
热议问题