Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find specific value in Qmap

Tags:

c++

qt

qmap

I need to know in QMap second value there is. Functions like first() and last() are useless. Do i have to use iterator, some kind of loop?

 QMap<int, QString> map;
 map.insert(1, "Mario");
 map.insert(2, "Ples");
 map.insert(3, "student");
 map.insert(4, "grrr");
like image 992
mario Avatar asked Nov 30 '22 01:11

mario


2 Answers

If you find specific key or value you can do something like this:

// Returns 1
int key1 = map.key( "Mario" );

// returns "student"
QString value3 = map.value( 3 );

or do you want to iterate over all items in QMap?

like image 95
eferion Avatar answered Dec 05 '22 09:12

eferion


You don't need to iterate over the elements. First get all values via values() and then use contains().

bool ValueExists(const QMap<int, QString> map, const QString valExists) const
{
    return map.contains(valExists);
}
like image 20
Ezee Avatar answered Dec 05 '22 09:12

Ezee