多重映射是否保留相同键的插入顺序的问题已经被问了很多次,我认为很明显标准说它不保留。 虽然显然C++0x 有一段时间说它做到了,然后又没有了?
无论如何,我需要一个可以通过键查找的结构,并按相同键的插入顺序迭代它。这意味着我不能使用复合键,那么我如何通过按键查找呢?
那么,VC++ 是否维护多重映射相同键的插入顺序? (或者我在哪里查找 Microsoft 的实现,因为我找不到它?)如果没有,最好的替代方案是什么?提升::多索引?
如果我不只是在没有位置的情况下插入,总是先查找所需的位置,然后传递相同键的最后一个元素作为位置建议(例如使用 equal_range 的第二个迭代器),该怎么办?这也许可行(我只关心 g++ 和 Windows 编译器)我猜因为它可以与该元素进行比较,然后是下一个,然后高兴地插入?或者它不能,因为树结构可能意味着它实际上可能不会对相同的键进行神奇的叶子切换,并且如果错误的子项空闲,则将其插入那里?
顺便说一句,我从来不理解建议使用两种结构的答案。这肯定会让删除变得相当昂贵吗?
The question whether a multimap preserves the insertion order of identical 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?
发布评论
评论(2)
std::map; >
满足您的所有要求。std::map<std::vector<T> >
fulfills all your requirements.您确实可以使用复合键。只需使用
lower_bound
进行查找即可。但不确定这是否解决了您的主要问题。
You do can use compound keys. Just use
lower_bound
for lookup.Not sure if this solves your main problem though.