2013-07-05 47 views
4

我需要遍歷QMultiHash並檢查與每個鍵對應的值列表。我需要使用可變的迭代器,以便我們可以從哈希中刪除項目,如果他們符合某些標準。 The documentation沒有解釋如何訪問所有的值,只是第一個。此外,該API僅提供value()方法。如何獲得特定密鑰的所有值?如何迭代QMultiHash中的所有值()

這就是我想要做的事:

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 
    } 
} 

回答

1

可以更好地使用最新的文檔: http://doc.qt.io/qt-4.8/qmultihash.html

特別是:

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

對於未來的旅客,這是我最終做了什麼以繼續使用Java風格迭代器:

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 
    } 
} 
相關問題