迭代器解除引用断言错误

时间:2017-11-02 19:23:49

标签: c++

我正在使用迭代器循环遍历列表,然后将它们解除引用到同一时间的obj或数组中,我在2个不同的实例中执行此操作但是我收到一个断言错误,说我无法取消引用迭代器。

我很困惑,因为在其中一个函数中它工作正常,但是在第二个函数中它抛出了错误,但它们的编码方式相同。

抛出断言错误的函数:

Coord backTrack(){ // recalls to intersection where dead-end route was found
    list<Coord>::iterator it = pathHistory.end();
    Coord deadEnd = *it;
    coordsToUnmark.push_back(deadEnd);
    pathHistory.pop_back();
    return pathHistory.front();
}

在这个函数中它工作正常:

int findPath (Maze& theMaze, const Coord& start, const Coord& end, Coord path[]){
    patherFinder(theMaze, start.x, start.y, end);
    list<Coord>::iterator it;
    int ii = 0;
    for (it = pathHistory.begin(); it != pathHistory.end(); it++){
        path[ii] = *it;
        ii++;
}

1 个答案:

答案 0 :(得分:0)

阅读完评论后,我意识到list.end()不是列表容器中过去元素的最后一个元素,它解释了为什么我不能解除引用它。