I am trying to put values from a QMap into a QVector, but program crash and I get no good debug information. I am not used to C++ container classes and assume I have done something illegal. The code is shown below:
MyClass::MyClass()
{
myQMap = new QMap<QString, QString>();
}
void someFunc(QString data1, QString data2)
{
QVector<double> a(30);
QVector<double> x(30);
myQMap->insert(data1, data2);
QMap<QString, QString>::const_iterator i = myQMap->constBegin();
while(i != myQMap->constEnd())
{
a.append(i.key().toDouble());
x.append(i.value().toDouble());
}
}
It seems that your loop will be "endless" in the sense that you are not advancing the iterator instance. That means, you will shortly append 30 items and the a
and x
vector container will be saturated, and then you will append further on... you cannot do this without any issues.
The simple fix is to increment the iterator by using something like the following snippet:
QMap<QString, QString>::const_iterator i;
for (i = myQMap->constBegin(); i != myQMap->constEnd(); ++i)
{
a.append(i.key().toDouble());
x.append(i.value().toDouble());
}
That being said, I am not sure why you are doing this iterator logic manually when you could just use foreach as follows:
foreach (const QPair<QString, QString> &myPair, myQMap)
{
a.append(myPair.first.toDouble());
x.append(myPair.second.toDouble());
}
Disclaimer: I have not tried to compile this code, but it should demonstrate the concept.