简体   繁体   English

迭代器取消引用断言错误

[英]Iterator dereferencing assertion error

I'm using an iterator to loop through list's and then dereferencing them into a obj or array of the same time, I am doing this in 2 different instances but I am getting an assertion error saying I cannot dereference the iterator. 我使用迭代器遍历列表,然后同时将它们解引用到obj或数组中,我在2个不同的实例中执行此操作,但是却收到一个断言错误,说我无法解引用迭代器。

I'm confused because in one of the functions it works fine, however in the second function it's throwing the error but they have been coded the same way. 我很困惑,因为在其中一个函数中它可以正常工作,但是在第二个函数中,它引发了错误,但是它们的编码方式相同。

the function the assertion error is being thrown: 引发断言错误的函数:

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();
}

in this function it works fine: 在此功能中可以正常工作:

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++;
}

阅读注释后,我意识到list.end()并不是列表容器中past-the-end元素的最后一个元素,这解释了为什么我无法取消引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM