列表迭代器不可取消引用
List Iterator not dereferenceable
我一直在为用户输入机场名称的机场开发一种成本最低的算法,然后我运行该算法来吐出你可以去的所有目的地和成本最低的目的地,包括转机。我使用列表迭代器从源位置遍历可到达的目的地,但在一次迭代后,代码中断,出现一条消息,告诉我迭代器不可取消引用。这是我的代码
//Finds minimum cost
void findPaths(std::string source)
{
std::list<int> Reachable;
int min = INTMAX_MAX;
int lowestIndex = -1;
bool existsInList = true;
std::stack<std::string> connectingFlights;
//Make arrays
//Initialize costs to a high value so any value will be smaller
int costs[MAX]{INTMAX_MAX};
//Initialize paths to negative one so that we know there is no location
int path[MAX]{ -1 };
//Find where the source is
int srcIndex = findOrInsert(source);
//Put the costs into the array, leaving the high number for where there is no path
for (int i = 0; i < MAX; i++)
{
costs[i] = priceEdges[srcIndex][i];
}
//Put the source index in places that have a path
for (int i = 0; i < MAX; i++)
{
if (priceEdges[srcIndex][i] == 0)
{
path[i] = -1;
}
else
{
path[i] = srcIndex;
Reachable.push_back(i);
}
}
//If the list is empty, we are done;
while (!Reachable.empty())
{
//Find the index that has the lowest value in costs
for (std::list<int>::iterator it = Reachable.begin(); *it < Reachable.size(); it)
{
if (costs[*it] < min)
{
min = costs[*it];
int lowestIndex = *it;
}
//Remove the index with the lowest value in costs
Reachable.erase(it);
//Save the previous cost to compare after a change may occur
int prevCost = costs[lowestIndex];
//Assign the value to the lowest cost it can find
costs[lowestIndex] = FindMin(costs[lowestIndex], costs[srcIndex] + priceEdges[srcIndex][lowestIndex]);
//If the price has changed
if (prevCost != costs[lowestIndex])
{
path[lowestIndex] = srcIndex;
}
existsInList = std::find(Reachable.begin(), Reachable.end(), lowestIndex) != Reachable.end();
if (!existsInList)
{
Reachable.push_back(lowestIndex);
}
}
}
您的for
循环完全错误。您在不验证迭代器是否有效的情况下取消引用迭代器,并将迭代器引用的目标值与向量的size进行比较,这毫无意义,因为它们是两个完全不同的东西。
您需要将循环替换为以下内容:
for (std::list<int>::iterator it = Reachable.begin(); it != Reachable.end(); )
甚至这个:
std::list<int>::iterator it = Reachable.begin();
while (it != Reachable.end())
然后,为了满足循环的停止条件,您需要更改以下行:
Reachable.erase(it);
相反:
it = Reachable.erase(it);
您正在从list
中删除一个项,这将使迭代器无效,但您永远不会更新迭代器以指向下一个项。因此,当代码再次尝试取消引用迭代器时,它将出现问题。erase()
将迭代器返回到列表中要删除的项之后的下一个项。
此外,在这条线上:
int lowestIndex = *it;
您正在声明一个新的临时变量,该变量随后立即超出范围,因此永远不会使用它。您有一个在函数开始时声明的前一个lowestIndex
变量,初始化后从未为其赋值,因此它总是-1。您需要从分配中删除int
:
lowestIndex = *it;
//Remove the index with the lowest value in costs Reachable.erase(it);
这将使迭代器无效,但for循环执行*it < Reachable.size()
,从而取消引用无效的迭代器。相反,应该这样做。
it = Reachable.erase(it);
此外,*it < Reachable.size()
可能应该被it != Reachable.end()
取代。
最后,for
循环的增量部分可能应该是空的,因为它没有做任何事情。您也可以使用while循环。
auto it = Reachable.begin();
while (it != Reachable.end())
{
// ...
it = Reachable.erase(it);
// ...
}
相关文章:
- C++取消引用指针.为什么会发生变化
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 取消引用运算符不能重载
- 了解通过引用传递取消引用指针时C++堆/堆栈分配
- 对于实际指针类型,用于检测类似指针(可取消引用)类型的模板函数失败
- 升压::可选<bool>取消引用
- C++如何将类方法传递给要求引用可调用的函数
- 结构引用和取消引用运算符
- 使用引用和取消引用运算符读取代码行时遇到问题
- 如何确定类型在 C++03 中是否可取消引用
- 我们应该在通过引用之前取消引用指针吗
- 数组迭代器不是可取消引用的错误
- 取消引用指向取消引用类中的函子的指针
- C++/CLI 项目如何引用可移植类库
- 如何在 boost::asio 线程池中使任务可取消/可中断
- 迭代器不是可取消引用的堆栈(表达式树)和循环无法正常工作
- 如何使用boost::expression-static在语义操作中引用可选子匹配
- 何时引用和取消引用具有成员函数的指针
- 列出迭代器而不是可取消引用的运行时
- 为什么持有引用可复制的类