QMap和QMultiMap中的项目顺序 [英] Order of items in QMap and QMultiMap

查看:859
本文介绍了QMap和QMultiMap中的项目顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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

使用QMap时,项目始终按键排序

With QMap, the items are always sorted by key

但它没有说明是否/如何对相等的键进行排序.

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文档:

与QHash不同,QHash以任意顺序存储其项,即QMap 存储其按键排序的项目.共享相同密钥的项目 (因为它们是使用QMap :: insertMulti()插入的,或者是由于 unite())将连续显示,从最近到 最近最少插入的值.

Unlike QHash, which stores its items in an arbitrary order, QMap stores its items ordered by key. Items that share the same key (because they were inserted using QMap::insertMulti(), or due to a unite()) will appear consecutively, from the most recently to the least recently inserted value.

所以看来QMap保持键相等的线对的相反插入顺序.

So it seems that QMap keeps the reversed insertion order of pairs with equal keys.

这篇关于QMap和QMultiMap中的项目顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆