qmlqmultimap

How to get key from value in QMultiMap?


I have QMultiMap as follows:

QMultiMap <int, QString> paramIDMap, paramValueMap;

My value is "xyz" and i want to take it's key.

Example: paramIDMap.getkey("xyz") like this

How to do this?

Expected output should return key.


Solution

  • QMultiMap is intended to store key-value pairs for fast lookup by key where a key can have multiple values. The QList QMap::keys(const T &value) const method which is inherited from QMap, will return a QList of keys for a specific value. That won't be fast lookup and the time complexity would be linear.

    QMultiMap <int, QString> paramIDMap;
    paramIDMap.insert(1,"a");
    paramIDMap.insert(1,"b");
    paramIDMap.insert(2,"a");
    paramIDMap.insert(2,"b");
    QList<int> ks = paramIDMap.keys("a");
    

    Which ks will contain [1,2].