When to select std :: vector via std :: map for key-value data?

Given the positive effects of caching and locality of data when searching in primary memory, I try to use std::vector<>with std::pair<>-like key values ​​and perform a linear search for both, if I know that the total amount of key values ​​will never be "too large" to seriously affect performance .

Recently, I have had many situations where I know in advance that I will have a huge number of elements with a key and therefore have preferred std::map<>from the very beginning.

I would like to know how you make decisions for the proper container in situations like those described above.

You

  • always use std::vector<>(or similar)?
  • always use std::map<>(or similar)?
  • Is there a gut feeling where in the frame of reference elements are preferable to another?
  • something completely different?

Thank!

+5
source share
5 answers

I rarely use std::vectorwith linear search (except in combination with binary search, as described below). I believe that for a small amount of data it would be better, but with such small data it is unlikely that something will create a huge advantage.

std::vector . A std::map , . , , (.. , ).

, , , (, std::lower_bound, std::equal_range). - (.. , a std::map). , , , - , - , , , , ( ) .

+7

(, ) "", , . ? ? ? .

+4

( unordered_map, - ) .

, , . , , .

, binary_search , .

+2

? , , - . - , , , / .

Of course, there is a lot of discussion about the effectiveness of maps versus lists / vectors (sorted and unsorted) - if your key is a string of 10,000 characters, it may take longer to compare strings than to search through a list of several elements, so you want make sure you can also compare keys efficiently.

+2
source

Why do not you accept unordered_map?

+1
source

All Articles