列表迭代器不可取消引用

List Iterator not dereferenceable

本文关键字:引用 可取消 不可取 迭代器 列表      更新时间:2023-10-16

我一直在为用户输入机场名称的机场开发一种成本最低的算法,然后我运行该算法来吐出你可以去的所有目的地和成本最低的目的地,包括转机。我使用列表迭代器从源位置遍历可到达的目的地,但在一次迭代后,代码中断,出现一条消息,告诉我迭代器不可取消引用。这是我的代码

//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);
    // ...
}