質問

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