开发者

Associating and iterating in C++

开发者 https://www.devze.com 2023-01-29 19:03 出处:网络
I\'ve got a situation where I want to use an associative container, and I chose to use a std::unordered_map, because it\'s perfectly feasible that this container could be used to hold millions or more

I've got a situation where I want to use an associative container, and I chose to use a std::unordered_map, because it's perfectly feasible that this container could be used to hold millions or more of elements. But now I also need to iterate in order. I considered having the value types link to each other in a list, but now I'm going to have issues with memory management.

Should I change container, say to a std::map? Or just iterate once through my unordered_map, insert into a vector, and sort, then iterate? It's pretty unlikely that I will need to iterate in an or开发者_运维技巧dered fashion repeatedly.


Well, you know the O() of the various operations of the two alternatives you've picked. You should pick based on that and do a cost/benefit analysis based on where you need the performance to happen and which container does best for THAT.

Of course, I couldn't possibly know enough to do that analysis for you.


You could use Boost.MultiIndex, specifying the unordered (hashed) index as well as an ordered one, on the same underlying object collection.

Possible issues with this - there is no natural mapping from an existing associative container model, and it might be overkill if you don't need the second index all the time.

0

精彩评论

暂无评论...
验证码 换一张
取 消