unsigned int j = 0;
openListIterator = openListVector.begin ();
while (exitPointDetailsVector[lowestWeightedPointInOpenList.pointId].branchesVector[m].connectedExitPoint >= openListVector[j].pointId)
&& (openListIterator <= openListVector.end()))
{
// Move the iterator.
openListIterator++;
// Move the index.
j++;
}
// Insert in the vector in the required position.
listStruct objOpenListStruct;
objOpenListStruct.pointId = exitPointDetailsVector[lowestWeightedPointInOpenList.pointId].branchesVector[m].connectedExitPoint;
objOpenListStruct.weight = exitPointDetailsVector[lowestWeightedPointInOpenList.pointId].weight + exitPointDetailsVector[lowestWeightedPointInOpenList.pointId].branchesVector[m].distance;
objOpenListStruct.parentPointId = exitPointDetailsVector[lowestWeightedPointInOpenList.pointId].exitPoint;
***********openListVector.insert (openListIterator, objOpenListStruct);
此代码位于for循环中。 但我已经在正确的行上进行了正确的迭代器初始化,但我仍然遇到了分段错误。
任何提示?
答案 0 :(得分:2)
在语句openListIterator <= openListVector.end()
中,如果您到达openListIterator == openListVector.end()
,那么您将遇到段错误,因为当代码到达openListIterator++
时,您的迭代器变得“超出范围”
尝试openListIterator != openListVector.end()
答案 1 :(得分:2)
这是一个提示。您不能在迭代器上使用运算符&lt; =
openListIterator <= openListVector.end()
因此,毕竟你的迭代器可能不太好。
答案 2 :(得分:2)
我的直接建议是:不要那样做。如果要按排序顺序维护项目集合,则应该使用std::set
(除非插入相对不常见)。如果您打算使用已排序的向量,您也可以利用它的排序并使用二进制搜索来查找插入点(例如,使用std::lower_bound
)。
当您需要/想要添加到集合的末尾时,您当前遇到的问题似乎是无效的迭代器。这个(以及其他问题)将使用预先打包的搜索算法(或std::set
或std::multiset
)自动处理。
答案 3 :(得分:1)
我想知道可能会产生什么影响:
&& (openListIterator <= openListVector.end()))
实际上,在while子句的第一部分中,您不使用openListIterator
,因此当openListIterator
为openListVector.end()
然后它时,循环就可能是enteres得到增加。因此,当您执行插入操作时,您会收到分段错误...
如果您遇到分段错误,可以检查这种情况吗?
如果这是问题的原因,您可以改为使用:
&& (openListIterator < openListVector.end()))