Consider the following code:

QMultiHash<int, Tag*>::iterator i = th.begin(); while(i != th.end()) {
    int key = i.key();
    Value* val = i.value();
    if(key == lastkey) {
        // Do something
    }
    else {
        // Do Something else
    }
}

Is the assumption that the entries with the same keys are returned consecutively by the iterator?

Thank you in advance.

有帮助吗?

解决方案

Is the assumption that the entries with the same keys are returned consecutively by the iterator?

Yes. See the code below demonstrating this.

main.cpp

#include <QMultiHash>
#include <QString>
#include <QDebug>

int main()
{
    QMultiHash<QString, int> stringIntMultiHash;
    stringIntMultiHash.insert("foo", 2);
    stringIntMultiHash.insert("bar", 5);
    stringIntMultiHash.insert("oo", 0);
    stringIntMultiHash.insert("bar", 132);
    stringIntMultiHash.insert("baz", 2131);
    stringIntMultiHash.insert("foo", 10);
    stringIntMultiHash.insert("stuff", 20);
    stringIntMultiHash.insert("baz", 30);

    for (QMultiHash<QString, int>::iterator i = stringIntMultiHash.begin(); i != stringIntMultiHash.end(); ++i)
        qDebug() << "Key:" << i.key() << "Value:" << i.value();
}

Output

g++ -Wall -fPIC -lQt5Core -I/usr/include/qt/QtCore -I/usr/include/qt main.cpp && ./a.out

Key: "bar" Value: 132 
Key: "bar" Value: 5 
Key: "stuff" Value: 20 
Key: "foo" Value: 10 
Key: "foo" Value: 2 
Key: "baz" Value: 30 
Key: "baz" Value: 2131 
Key: "oo" Value: 0
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top