c++ - 如何遍历 QMultiHash 中的所有 values()

标签 c++ qt qt4 iterator multimap

我需要遍历 QMultiHash 并检查与每个键对应的值列表。我需要使用可变迭代器,这样我就可以从哈希中删除符合特定条件的项目。 The documentation没有解释如何访问所有的值,只是第一个。此外,该 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
    }
}

最佳答案

对于 future 的旅行者,这就是我为了继续使用 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
    }
}

关于c++ - 如何遍历 QMultiHash 中的所有 values(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17497598/

相关文章:

c++ - 使用 QComboBox 在 QPushButton 上添加菜​​单

c++ - 从 QDialog 获取值

c++ - QDockWidget导致qt崩溃

c++ - 插入容器

c++ - 对数组开头的奇数进行排序

c++ - Qt:太多的setText调用导致延迟

c++ - glGenBuffers 在 Release Build 中崩溃

c++ - Frameles 和透明窗口不起作用! Qt 部件

c++ - 使用 Visual C++ 的窗口窗体应用程序中的 Sleep()

qt - 在 qmake 中运行 "source"bash shell 脚本