开发者

Multimap order of equal keys in VC++

开发者 https://www.devze.com 2023-03-20 16:12 出处:网络
The question whether a multimap preserves the insertion order of identical 开发者_StackOverflow社区keys has been asked numerous times and I think it is pretty clear that the standard says it doesn\'t.

The question whether a multimap preserves the insertion order of identical 开发者_StackOverflow社区keys has been asked numerous times and I think it is pretty clear that the standard says it doesn't. Although apparently C++0x for a while said it did and then didn't again?

Anyway, I need a structure on which I can look up by key, and iterate over it in insertion order for identical keys. Which means I can't use compound keys, as then how could I look up by key?

So, does VC++ maintain the insertion order of identical keys for multimaps? (Or where would I look up Microsofts implementation, because I couldn't find it?) If it doesn't what is the best alternative? boost::multiIndex?

What if I were to instead of just inserting without position always first lookup the desired position, and pass the last element for an identical key as position suggestion (e.g. use the second iterator of equal_range)? This could maybe work (I only care about g++ and Windows compiler) I would guess as it could compare with that element, then the next, then be happy and insert? Or it could not, as the tree structure might mean it might actually not do the magic leaf switching for identical keys and if the wrong child is free insert it there?

By the way, I never understand the answers that suggest using two structures. Surely that makes deletions quite expensive?


std::map<std::vector<T> > fulfills all your requirements.


You do can use compound keys. Just use lower_bound for lookup. Not sure if this solves your main problem though.

0

精彩评论

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