-1

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.

Cm Jagtap
  • 147
  • 1
  • 2
  • 10
  • Which key? ____ – LogicStuff Nov 06 '19 at 12:52
  • You can't. That's not what maps are for. Associative containers don't work this way. There could be multiple keys with the same value. Your only option is to iterate over all the keys in the map, and check each key's value, and figure out what to do from there. But if you need to find a key for a value in the map, it means that you're using the wrong container. You should not be using a map, but something else. You will have to figure out, by yourself, which container you can use in order to do whatever you need to do. – Sam Varshavchik Nov 06 '19 at 12:53
  • @ LogicStuff It's hashmap so we retrieve hashmap key from it's value. in the same way this qmultimap works . so i have inserted record like paramIDMap.insert(key,value); – Cm Jagtap Nov 06 '19 at 12:54
  • @SamVarshavchik Okay got it – Cm Jagtap Nov 06 '19 at 12:56

1 Answers1

2

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].

Soheil Armin
  • 2,725
  • 1
  • 6
  • 18
  • 1
    @cmhacker, just another note: Qt documentation for each class, will not show the inherited methods, and properties if that class is a sub-class. You will find them by clicking ```List of all members, including inherited members``` link which is always below the first table. It seems to be the reason you did not find this method. – Soheil Armin Nov 07 '19 at 07:13