问题描述
我想使用QMultiMap
(从QMap
派生)来存储键/值对.由于我可以多次拥有键,因此我更愿意使用QMultiMap
.
I would like to use QMultiMap
(which is derived from QMap
) to store key/value pairs. Since I can have keys multiple times I would prefer to use QMultiMap
.
假设我将按照给定的顺序插入以下对:
Assume I would insert the following pairs in the given order:
"C" -> 5
"A" -> 10
"B" -> 77
"B" -> 1
"X" -> 314159
在地图上进行迭代时(最好使用Java样式的迭代器),我需要保留等键对的顺序. IE.迭代时,"B" -> 77
和"B" -> 1
应该完全按插入顺序显示.键之间的顺序不同并不重要.
When iterating over the map (using java style iterators preferably) I need the order of equal-key-pairs to be preserved. I.e. "B" -> 77
and "B" -> 1
should appear exactly in insertion order when iterating. The order between keys that are different does not matter.
不幸的是,文档没有提供有关该细节的信息.它说
Unfortunately the documentation doesn't tell something about that detail. It says
但它没有说明是否/如何对相等的键进行排序.
but it does not say if/how it sorts equal keys.
QMap
会保留具有相同键的对的插入顺序吗?还是可以某种方式保留它?
Does QMap
preserve the insertion order of pairs with equal keys or can it be preserved in some way?
推荐答案
来自有关QMap::iterator
的Qt文档:
所以看来QMap
保持键相等的线对的相反插入顺序.
So it seems that QMap
keeps the reversed insertion order of pairs with equal keys.
这篇关于QMap和QMultiMap中的项目顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!