How To print all member QMap with correct key
-
i need the correct key , how to fix my code , i'm using
devnum_bbu_slotid.key(slot_id)
for getting key, here's my snipet .thanks.
data:
QMap(("L1071905001", 6)("L1071905001", 3)("L1081905001", 7)("L1081905001", 6))
code:
QMap<QString,int> devnum_bbu_slotid; foreach (int slot_id , devnum_bbu_slotid){ qDebug()<<"-- key:"<<devnum_bbu_slotid.key(slot_id)<<" value:"<<slot_id; }
output :
-- key: "L1071905001" value: 6 -- key: "L1071905001" value: 3 -- key: "L1081905001" value: 7 -- key: "L1071905001" value: 6
-
@rifky said in How To print all member QMap with correct key:
i need the correct key , how to fix my code
? What do you mean? You add duplicate keys into the
QMap
, and that is what your code prints out. If you mean you want to know the key for, say, value of7
then obviously just print outdevnum_bbu_slotid.key(7)
? -
@JonB said in How To print all member QMap with correct key:
@rifky said in How To print all member QMap with correct key:
i need the correct key , how to fix my code
? What do you mean? You add duplicate keys into the
QMap
, and that is what your code prints out. If you mean you want to know the key for, say, value of7
then obviously just print outdevnum_bbu_slotid.key(7)
?i mean,
L1071905001 have 2 values and L1081905001 have 2 values ,
i want data printed like this,-- key: "L1071905001" value: 6 -- key: "L1071905001" value: 3 -- key: "L1081905001" value: 7 -- key: "L1081905001" value: 6
but this code print L1081905001 just have 1 value, because one of the values same(6) as L1071905001
-
@rifky
Ah, I see, with the keys you picked being so similar I didn't even notice that there are two different keys! You might have drawn the reader's attention to this!So instead of using
key()
("Returns the first key with value value") you need to call QList<Key> QMap::keys(const T &value) const, whichReturns a list containing all the keys associated with value value in ascending order.
This function can be slow (linear time), because QMap's internal data structure is optimized for fast lookup by key, not by value.
So you get a list of all the keys with that value, and do what you will with those.
Or, don't try to lookup by value (you'll need to know all the values), just iterate the whole
QMap
and you will meet the multiple keys which (happen to) have the same value. There is no "correct key" for a value of6
, there are simply multiple keys with that value. -
Hey, I assume that you are using
insertMulti()
to add values since you have different values with the same key.
That makes youQMap
actually aQMultiMap
.
So referring to the foreach doc:QMultiMap<QString, int> map;
...
foreach (const QString &str, map.uniqueKeys()) {
foreach (int i, map.values(str))
qDebug() << str << ':' << i;
}Your code should be
foreach (const QString &key, devnum_bbu_slotid.uniqueKeys()) { foreach (int slot_id, devnum_bbu_slotid.values(key)) qDebug()<<"-- key:"<<key<<" value:"<<slot_id; }
-
@rifky
As per @Bonnie, if you are not using aQMultiMap
(you don't seem to use that) you do not have any multiple values against a given key in aQMap
: the last one added replaces the previous one. If you do want multi-values per key, you must change over toQMultiMap
, and then you can use @Bonnie's code. -
@JonB said in How To print all member QMap with correct key:
if you are not using a QMultiMap (you don't seem to use that) you do not have any multiple values against a given key in a QMap
Actually he can...
QMap::insertMulti
is obsolete, but still exists... -
Sorry for late respon, i try to change this code , i've found another solution, before i'm using
devnum_bbu_slotid.insertMulti
QMultiMap<QString,int> devnum_bbu_slotid; QMapIterator<QString, int> x(devnum_bbu_slotid); while (x.hasNext()) { x.next(); qDebug()<<"-- key:"<<x.key()<< "value:" <<x.value(); }
it's worked
-
@Bonnie
The help page for the latest (https://doc.qt.io/qt-5/qmap.html) has noQMap::insertMulti
, and statesNormally, a QMap allows only one value per key. If you call insert() with a key that already exists in the QMap, the previous value will be erased. For example:
However, you can store multiple values per key by using using the subclass QMultiMap. If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:So we wouldn't want to encourage the OP to use any undocumented/obsolete API, would we? ;-)
-
@JonB said in How To print all member QMap with correct key:
The help page for the latest (https://doc.qt.io/qt-5/qmap.html) has no QMap::insertMulti
It is in the
Obsolete members
page.So we wouldn't want to encourage the OP to use any undocumented/obsolete API, would we? ;-)
Of course!
I was just assuming the OP is already using it so he may think what you said is not true... -
@JonB said in How To print all member QMap with correct key:
@Bonnie
The help page for the latest (https://doc.qt.io/qt-5/qmap.html) has noQMap::insertMulti
, and statesNormally, a QMap allows only one value per key. If you call insert() with a key that already exists in the QMap, the previous value will be erased. For example:
However, you can store multiple values per key by using using the subclass QMultiMap. If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:So we wouldn't want to encourage the OP to use any undocumented/obsolete API, would we? ;-)
i'm using QC 4.11, its documented well
QMap::iterator QMap::insertMulti(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
If there is already an item with the same key in the map, this function will simply create a new one. (This behavior is different from insert(), which overwrites the value of an existing item.)
See also insert() and values().@Bonnie said in How To print all member QMap with correct key:
@rifky
Right! Iterator is the first thing comes to my mind.
But I think maybe you preferforeach
:)yeah ,, i got 2 solution at the same time , i'll use your code ,, that's more readable for me :)
solved, thanks @JonB and @Bonnie for the quick response and helping me