[Interest] QMultiHash / QMultiMap: insert QList?
Tony Rietwyk
tony at rightsoft.com.au
Thu Oct 8 14:05:40 CEST 2026
Hi,
On 8/10/2026 6:24 pm, Schimkowitsch Robert wrote:
>
> Hi there,
>
> I saw this data structure:
>
> QHash<SomeKey, QList<SomeValue>>
>
> …and I was wondering: Why not use QMultiHash?
>
> The rationale was that it’s easier to extract and re-insert whole
> blocks (i.e. all values sharing the same key), which is indeed a
> relevant use case for the surrounding code.
>
> There IS QHash::values(key) -> QList<value>, but what’s missing is the
> opposite function: insert(key, QList<value>)
>
QMultiHash has the interesting property that the multiple values are
implicitly ordered with the last to be inserted returned first in
.values(). This presents problems when inserting or replacing from a
list. A naive forward implementation would end up reversing the order
when later fetched by .values():
hash.insert(1, v1)
hash.insert(1, v2)
auto list = hash.values() returns (V2, V1)
A new function hash.insert(1, list), followed by hash.values() now
returns (V1, V2, V2, V1)!
Ditto with a new replace(1, list), followed by hash.values() now returns
(V1, V2).
That reversal would probably catch a lot of people off guard.
Regards,
Tony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20261008/a6aec022/attachment.htm>
More information about the Interest
mailing list