使用while循环时出现问题
An issue when looping with while
我有一个向量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::中的ASSERT失败,位于"索引超出范围"。。。
当我删除while
循环(计算将出错)时,应用程序不再崩溃。
由于i<t和j可能等于i+1,在访问a[j]时可能产生超出范围的误差。也就是说,有时j=t,所以你试图访问a[t]。它看起来不正确。
也许放是正确的
while( j < i)
而不是
while( j <= i)
相关文章:
- 退出简单while循环时出现问题
- 嵌套While循环不起作用(C++问题)
- 使用 GMP 的 while 循环出现问题
- 为什么我在这个函数中的while循环出现问题
- while 循环的问题
- C++字符串和 while 语句的问题
- C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?
- 如何解决 while 循环中数组过载的问题
- 我在while循环中遇到问题,我只是无法弄清楚为什么它无法按预期工作
- 我的代码中关于while循环和数组的问题
- C多行宏问题:为什么不使用if(1){..}而不是do{..}while(0)在多行宏定义中
- 嵌套 while 循环,包含文件 c++ 问题
- 遇到 while 循环问题
- 涉及while循环和OR语句的新手C++问题
- 使用while循环输入的小问题
- 在结束取决于布尔值的 do/while 循环时遇到问题 - C++
- 随机化不会在 while 循环中发生。如何解决这个问题?C++
- 从嵌套的 for 循环转换为嵌套的 while 循环时出现问题
- C++:这个while循环有什么问题
- 可执行文件C++问题:while 循环和 if/else if/else