Search code examples
c++qtqmap

access QMap element by its value not by its key


i want to access a QMap by it's value, but i don't want to iterate over it and find element with same value and use it's key, is there anyway to find QMap key by it's content? my code is :

    QMap<int, QVector<QString> >::iterator it;
QMap <QString, int> m_all_data;

i want to access to element of m_all_data with value of my iterator key;


Solution

  • You can use:

    const Key QMap::key ( const T & value ) const 
    

    which returns the first key with value value or

    QList<Key> QMap::keys ( const T & value ) const
    

    which returns a list containing all the keys in the map in ascending order.

    But it is slow (linear time), because QMap's internal data structure is optimized for fast lookup by key, not by value.