[c++] C ++无法删除链接列表中的节点



0 Answers

Question

我正在尝试使用mylist.erase()删除链接列表中的节点; 但节点仍然在列表中。 我试图使用删除(),但程序崩溃。 任何想法?

list <Person*> :: iterator it;
it = gamelist.begin(); //gamelist is a <Person*> list. it is an iterator to this list.
while (it!=gamelist.end()){
    if ((*it)->is_dead == true) {
        delete (*it); //if I comment this line the program does not crash but the "dead" Person still remains in the list.
        it = gamelist.erase(it);
    }
    else ++it;
}





Related