vector or map, which one to use?

前端 未结 7 1418
渐次进展
渐次进展 2020-11-28 03:00

I\'ve heard many people say that if the number of expected elements in the container is relatively small, it is better to use std::vector instead of std::

相关标签:
7条回答
  • 2020-11-28 03:48

    Basically, maps are used for lookup.

    But, sometimes std::vector can be used instead of std::map even for look up.

    If there are going to be very less elements in your key-value pairs, then you can go for an iterative search using key even in std::vector<std::pair<x,y>>.

    This is because of the fact that hashing takes time, especially for hashing strings and for other operations in map like storing data in heap.

    You would only see a better difference in std::map, if you have more elements in which you have to lookup and also when you want to do frequent lookup in the list of elements that you have.

    0 讨论(0)
提交回复
热议问题