"j" is created every time through the outer while loop and ceases to exist before the next loop starts. There is no "reinitialize" they are separate instances in each outer loop. I have no idea what you mean by "j will have extra values". This:
#include <QtCore>
#include <QDebug>
QHash<QString, QHash<int,float> > testhash;
int main(int argc, char **argv) {
// code to populate hashes
for (int i = 0; i < 4; ++i ) {
testhash[key] = QHash<int,float>();
for (int j = 0; j < 3; ++j) {
testhash[key][j] = j * 3.0 ;
}
}
QHashIterator<QString, QHash<int,float> > i(testhash);
while (i.hasNext())
{
i.next();
qDebug() << "Outer key" << i.key();
QHashIterator<int,float> j(i.value());
while (j.hasNext())
{
j.next();
//get data
qDebug() << "Inner" << j.key() << j.value();
}
}
}
#include <QtCore>
#include <QDebug>
QHash<QString, QHash<int,float> > testhash;
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
// code to populate hashes
for (int i = 0; i < 4; ++i ) {
QString key = QString("Key %1").arg(i);
testhash[key] = QHash<int,float>();
for (int j = 0; j < 3; ++j) {
testhash[key][j] = j * 3.0 ;
}
}
QHashIterator<QString, QHash<int,float> > i(testhash);
while (i.hasNext())
{
i.next();
qDebug() << "Outer key" << i.key();
QHashIterator<int,float> j(i.value());
while (j.hasNext())
{
j.next();
//get data
qDebug() << "Inner" << j.key() << j.value();
}
}
}
To copy to clipboard, switch view to plain text mode
outputs nothing unexpected.
Outer key "Key 0"
Inner 0 0
Inner 1 3
Inner 2 6
Outer key "Key 1"
Inner 0 0
Inner 1 3
Inner 2 6
Outer key "Key 2"
Inner 0 0
Inner 1 3
Inner 2 6
Outer key "Key 3"
Inner 0 0
Inner 1 3
Inner 2 6
Outer key "Key 0"
Inner 0 0
Inner 1 3
Inner 2 6
Outer key "Key 1"
Inner 0 0
Inner 1 3
Inner 2 6
Outer key "Key 2"
Inner 0 0
Inner 1 3
Inner 2 6
Outer key "Key 3"
Inner 0 0
Inner 1 3
Inner 2 6
To copy to clipboard, switch view to plain text mode
Bookmarks