List of key-value pairs that can return both keys and values
-
Hey guys, I run into the following issue:
I have some key-value pairs like this:
/*On the left I have my "keys" of the Type QString on the right I have my "values" of the Type QBluetoothUuid*/ "Device Information Service": 6e400003-b5a3-f393-e0a9-e50e24dcca9e, "Battery Status": 6e400002-b5a3-f393-e0a9-e50e24dcca9e, "Device Name Characteristic": 6e500003-b5a3-f393-e0a9-e50e24dcca9e
Now I need a function that returns me the key, If I pass the value. And If i pass the key, I should get the value. For example:
/*Assuming that my function is called getTheData()*/ getTheData("Device Information Service"); //output (type: QBluetoothUuid): 6e400003-b5a3-f393-e0a9-e50e24dcca9e getTheData(6e400003-b5a3-f393-e0a9-e50e24dcca9e); //output (type: QString): "Device Information Service"
How can this be done in Qt (and C++)? Maybe in a List of pairs (or another Collection)? But because I have different return types, do I need two (maybe overloaded) functions?
-
-
@Christian-Ehrlicher said in List of key-value pairs that can return both keys and values:
Thank you. So I would have something like:
QMap<QString, QBluetoothUuid> dataMap; dataMap["Device Information Service"] = QBluetoothUuid(QUuid(6e400003-b5a3-f393-e0a9-e50e24dcca9e)); /*Retrieving the Uuid that belongs to my Device Information Service:*/ qDebug() << dataMap["Device Information Service"]; //output Should be {6e400003-b5a3-f393-e0a9-e50e24dcca9e}
But how Can I retrieve the value that belongs to the specified Uuid? Because I think that this will not work:
qDebug() << dataMap[QBluetoothUuid(QUuid(6e400003-b5a3-f393-e0a9-e50e24dcca9e))];
-
@SpaceToon said in List of key-value pairs that can return both keys and values:
But how Can I retrieve the value that belongs to the specified Uuid?
Did you actually read my links? There are a lot of functions...
-
@SpaceToon said in List of key-value pairs that can return both keys and values:
QMap<QString, QBluetoothUuid> dataMap;
Please keep in mind that a QMap (or QHash) is a ONE way container: you can only look up by key.
If you want to look up also by value, i.e. do a reverse lookup, you'll need a "bimap", and you have 2 options I guess:
- use external support for such structure, i.e. Boost Bimap
- use another QMap (or QHash) to use the device Id as key
QMap<QBluetoothUuid, QString> deviceIdMap;
Using option #2 requires that you need to maintain 2 containers at the same time, i.e. if you need to remove device Id "12345" then you need to remove as well the entry in the other container matching that device Id.
Edit: you may want to look at this Stackoverflow Q&A
-
@Pablo-J-Rogina said in List of key-value pairs that can return both keys and values:
you can only look up by key.
No, you can also look up by value, see the docs. Although it's not very optimal but the OP did not tell us how much entries he has. For a small value it's negligible.
-
@Christian-Ehrlicher said in List of key-value pairs that can return both keys and values:
No, you can also look up by value, see the docs
Could you please elaborate about that? I couldn't find it.
-
@Christian-Ehrlicher thank you for the enlightment...
It was just the habit of using key as the lookup approach.
Anyways, I guess the OP has everything at hand to solve the issue. -
Thank you both very much. I have 20 key-value-pairs, the values are constant, no more are added and none are removed or changed.
@Christian-Ehrlicher do you think, that 20 pairs are too much so that the QMap would be slow? -
Hi,
@SpaceToon said in List of key-value pairs that can return both keys and values:
Thank you both very much. I have 20 key-value-pairs, the values are constant, no more are added and none are removed or changed.
@Christian-Ehrlicher do you think, that 20 pairs are too much so that the QMap would be slow?If you are having speed issue with 20 elements then it's likely somewhere else.
What makes you think that a slowdown may happen ?
-
20 list, map or array elements are nothing if you iterate correctly.
Here are several ways to iterate through a
QMap
https://doc.qt.io/qt-5/qmap.html#details -
@SGaist Hey, because in the doc is written (for
const Key QMap::key(const T &value, const Key &defaultKey = Key()) const
):This function can be slow (linear time), because QMap's internal data structure is optimized for fast lookup by key, not by value.
Therefore I thought there could be performance issues. But I tested it and I did not recognize any performance disadvantages.
20 list, map or array elements are nothing if you iterate correctly.
Here are several ways to iterate through a QMap
Thank you too!