When does using a std::multimap make sense

后端 未结 3 1935
天命终不由人
天命终不由人 2021-01-31 02:55

I am currently experimenting on some usage of stl-datastructures. However I am still not sure when to use which one and when to use a certain combination. Currently I am trying

3条回答
  •  不思量自难忘°
    2021-01-31 03:26

    A map of vectors comes with the memory overhead for the capacity of each vector. std::vector typically allocates space for more elements than you actually have. It may not be a big deal for your application, but it's another tradeoff you haven't considered.

    If you're doing a lot of reads, then the O(1) lookup time of unordered_multimap might be a better choice.

    If you have a reasonably modern compiler (and given the presence of the auto keyword, you do) then in general you're going to have a difficult time beating the standard containers in terms of performance and reliability. The people who wrote them are experts. I would always start with the standard container that most easily expresses what you want to do. Profile your code early and often, and if it's not running fast enough, then look for ways to improve it (e.g., using the unordered_ containers when doing mostly reads).

    So, to answer your original question, if you need an associative array of values where those values won't be unique, then using std::multimap definitely makes sense.

提交回复
热议问题