0

我有一个向量QVector<float> dist;,我在其中保持所有维度的欧几里得距离。我保持如下尺寸:

QHash<int, QVector<float> > hash; 

键在哪里int,值再次保存在QVector<float>. 我尝试填写时的代码dist如下:

for(int i = 0; i < t; i++)
    {
        for(int j = 1; j < t; j++)
        {
            while( j <= i)
                j++;
            dist.push_back(qPow((a[i] - hash[i].at(point)), 2) + qPow((a[j] - hash[j].at(point)), 2));
            qDebug() << "Euclidean distance for Dim" << i << "and Dim" << j << " = " << dist[i];
        }
    }

循环按预期计算所有内容,但在以下情况下因内存错误而崩溃:

QVector::at "index out of range" 中的 ASSERT 失败...

当我删除while循环(计算将是错误的)时,应用程序不再崩溃。

4

1 回答 1

1

Since i < t and j might be equal i+1 it is possible to generate out of range error while access to a[j]. I.e. sometimes j = t, so you try to access a[t]. It looks incorrect.

May be it will be correct to put

while( j < i)

instead of

while( j <= i)
于 2013-08-27T13:33:43.297 回答