请考虑以下代码:
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
}
}
是否假设迭代器连续返回具有相同键的条目?
提前谢谢。
答案 0 :(得分:0)
是否假设迭代器连续返回具有相同键的条目?
是。请参阅下面的代码来证明这一点。
#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();
}
g ++ -Wall -fPIC -lQt5Core -I / usr / include / qt / QtCore -I / usr / include / qt main.cpp&amp;&amp; ./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