开发者

QMap::insertMulti or QMultiMap?

开发者 https://www.devze.com 2023-02-03 05:18 出处:网络
What should i use between QMap::insertMulti and QMultiMap to 开发者_运维知识库handle : 2 -> abc

What should i use between QMap::insertMulti and QMultiMap to 开发者_运维知识库handle :

2 -> abc

2 -> def

3 -> ghi

3 -> jkl

What's the difference enter the 2 solutions ?


Reading Container Classes:

QMap<Key, T>
This provides a dictionary (associative array) that maps keys of type Key to values of type T. Normally each key is associated with a single value. QMap stores its data in Key order; if order doesn't matter QHash is a faster alternative.

QMultiMap<Key, T>
This is a convenience subclass of QMap that provides a nice interface for multi-valued maps, i.e. maps where one key can be associated with multiple values.

it looks like both can do the job. In this document there is also Algorithmic Complexity section where you can see that both classes have the same complexity.

I would choose QMultiMap just to better document the fact I'm going to hold multiple values with the same key.


Both can serve this purpose. QMultiMap is actually a subclass of QMap.

If you are willing to have multiple values for single key, you can use: QMap : for inserting use insertMulti QMultiMap : for inserting use insert

If you are willing to have single value for single key, you can use: QMap : for inserting use insert QMultiMap : for inserting use replace

You can see that both can server both purpose. But, each have unique default behavior which matches its name. Also, each have some methods or operators which is convenient for single/multi.

It is better to choose type depending on your need. It is a good practice. For example, if you use QMap for storing single key multiple values, some other person who is going through your class members might get the impression that you are willing to save single key value pairs (from the data type) Similarly, if you use QMultiMap, anyone reading the definition can get the idea that the data will have multiple value for same key.

0

精彩评论

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