How to choose between map and unordered_map?

前端 未结 4 1728
一个人的身影
一个人的身影 2020-11-29 16:24

Suppose I wanted to map data with a string as the key. What container should I have chosen, map or unordered_map? unordered_map takes

4条回答
  •  甜味超标
    2020-11-29 16:57

    In what cases would it get to O(n)?

    if you have such a bad hash function which produces the same hash value for all input stirngs (i.e. produce collisions)...

    What container should I have chosen, map or unordered_map?

    It is always the questions of requirements and kind/amount of data do you have.

    When does a map get more time efficient than unordered_map?

    It is just different structures. You'd better to make a chiose to use one of them depending on your typical use cases (takeing in account what kind of data do you have and its amount)

    Does it hppaen when n is small?

    In case of small data amount everything depends on particular STL implementation... So sometimes even a plain vector/array could be faster than associative containers...

提交回复
热议问题