调试时矢量下标超出范围
vector subscript out of range when debugging
代码是构建最小堆的一部分。看来下面这句话出错了。
if ((child < (int)heap.size() - 1) && (heap[leftchild].cost > heap[leftchild + 1].cost))
但是我不知道如何管理它。有人提供帮助吗?谢谢!!
void heap_adjust_down(vector<rt_node> & rt, vector<h_node> & heap)
{
cout << "the" << heap.size() << "times" << endl;
cout << heap[heap.size()-1].id << endl;
int child;
for (int i = heap.size() / 2; i >= 0; i--)
{
cout << " into build heap, i = " << i << endl;
int leftchild = 2 * i + 1;
h_node temp_h;
for (temp_h = heap[i]; leftchild < (int)heap.size(); i = child)
{
leftchild = 2 * i + 1;
cout << " into percdown" << endl;
child = leftchild;
if ((child < (int)heap.size() - 1) && (heap[leftchild].cost > heap[leftchild + 1].cost))
{
child++;
}
if (heap[child].cost < temp_h.cost )
{
heap[i] = heap[child];
rt[i].h_pos = child;
}
else break;
cout << "i = " << i << endl;
}
heap[i] = temp_h;
rt[i].h_pos = i;
}
}
问题更可能是这一行:
if (heap[child].cost < temp_h.cost )
此行没有像您在上一个 if 语句中类似所做的那样检查child < size
。此外,在内部 for 循环的第二次迭代中,leftchild
至少会加倍(子项也是如此)。这似乎不是你的意图。(leftchild
将加倍,因为您在循环迭代器中设置i = child
。
内部的 for 循环需要重新考虑。例如,您的测试表达式测试该leftchild < size
,但随后您立即根据i
重新计算leftchild
,有点使该测试无效。
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 矢量下标超出SFML游戏中的范围
- 运行时错误:矢量下标超出范围:正在检查空集
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 编译期间矢量下标超出范围
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 对矢量来说是全新的.矢量下标超出范围
- C++十进制到十六进制(字符串下标超出范围)
- 创建结构体向量,表达式:向量下标超出范围
- 读取矢量时矢量下标超出范围
- 断言失败错误,C++中的矢量下标超出范围问题
- 如何在 Linux 上强制执行矢量下标超出范围的调试断言
- 表达式:矢量下标超出范围
- 矢量下标超出范围,带有嵌套的 for 循环
- 向量下标超出向量向量的范围
- gcc8.2如何启用警告:数组下标在数组范围之上[-warray-bounds]
- 向量下标出的范围错误.即使向量的索引大于访问数据的索引,也会发生误差
- std::vector<std::vector<int>>:调试断言失败。C++矢量下标超出范围保留内存
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- 行进立方体重建 - 向量下标超出范围