如何迭代QMultiHash中的所有值()
How to iterate over all values() in a QMultiHash
我需要对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
}
}
对于未来的旅行者来说,为了继续使用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
}
}
最好使用最新的文档: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;
}
您可以像在简单的QHash
:中一样迭代QMultiHash
的所有值
for(auto item = _myMultiHash.begin(); item != _myMultiHash.end(); item++) {
std::cout << item.key() << ": " << item.value() << std::endl;
}
只是如果有多个值使用同一个键,那么同一个密钥可能会出现多次。
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 迭代时从向量和内存中删除对象
- 如何在c++迭代器类型中包装std::chrono
- 带过滤器的现代迭代c++集合
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- C++矢量迭代
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 擦除while循环中迭代的元素
- 实现一个在集合上迭代的模板函数
- 对于set上的循环-获取next元素迭代器
- 在向量内的向量上迭代
- 为什么output_editor Concept不需要output_e迭代器标记
- TSP递归解的迭代形式
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 使用迭代器时如何访问对象在向量中的位置?
- std::vector::迭代器是否可以合法地作为指针
- 如何迭代QMultiHash中的所有值()