我需要对一个 QMultiHash
并检查与每个键对应的值列表。 我需要使用一个可突变的迭代器,这样我就可以从哈希中删除符合特定条件的项目。 文档中 并没有解释如何访问所有的值,而只是第一个。 此外,API只提供了一个 value()
方法。 我如何获得某个特定键的所有值?
这就是我想做的事情。
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
}
}
可能最好使用最近的文档。http:/doc.qt.ioqt-4.8qmultihash.html
特别是。
QMultiHash<QString, int>::iterator i = hash1.find("plenty");
while (i != hash1.end() && i.key() == "plenty") {
std::cout << i.value() << std::endl;
++i;
}
对于未来的旅行者来说,这就是我最终要做的事情 这样才能继续使用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
}
}
你可以遍历a的所有值。QMultiHash
俨然 QHash
:
for(auto item = _myMultiHash.begin(); item != _myMultiHash.end(); item++) {
std::cout << item.key() << ": " << item.value() << std::endl;
}
只是,如果有多个值使用同一键,同一键可能会出现多次。