Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QMap::insertMulti or QMultiMap?

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 ?

like image 784
canardman Avatar asked Jan 07 '11 10:01

canardman


2 Answers

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.

like image 90
Piotr Dobrogost Avatar answered Oct 06 '22 00:10

Piotr Dobrogost


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.

like image 33
Sarwar Erfan Avatar answered Oct 06 '22 00:10

Sarwar Erfan