How to iterate over all values ​​() in QMultiHash

I need to iterate over QMultiHash and check the list of values ​​corresponding to each key. I need to use a mutable iterator so that I can remove items from the hash if they meet certain criteria. The documentation does not explain how to access all the values, only the first. In addition, the API offers only the value() method. How to get all values ​​for a specific key?

This is what I am trying to do:

 QMutableHashIterator<Key, Value*> iter( _myMultiHash ); while( iter.hasNext() ) { QList<Value*> list = iter.values(); // there is no values() method, only value() foreach( Value *val, list ) { // call iter.remove() if one of the values meets the criteria } } 
+4
source share
2 answers

Better use the latest documentation: http://doc.qt.io/qt-4.8/qmultihash.html

In particular:

 QMultiHash<QString, int>::iterator i = hash1.find("plenty"); while (i != hash1.end() && i.key() == "plenty") { std::cout << i.value() << std::endl; ++i; } 
+1
source

For future travelers, this is exactly what I did to continue to use Java-style iterators:

 QMutableHashIterator<Key, Value*> iter( _myMultiHash ); while( iter.hasNext() ) { // This has the same effect as a .values(), just isn't as elegant QList<Value*> list = _myMultiHash.values( iter.next().key() ); foreach( Value *val, list ) { // call iter.remove() if one of the values meets the criteria } } 
+2
source

Source: https://habr.com/ru/post/1489990/


All Articles