C++ 与c+有问题+;stl列表
我有以下功能C++ 与c+有问题+;stl列表,c++,list,stl,C++,List,Stl,我有以下功能 void BulletFactory::update(Uint32 ticks) { std::list<Sprite*>::iterator it = activeBullets.begin(); while (it != activeBullets.end()) { (*it)->update(ticks); if(!((*it)->inView())) { activeBullets.remove(*it);
void BulletFactory::update(Uint32 ticks) {
std::list<Sprite*>::iterator it = activeBullets.begin();
while (it != activeBullets.end()) {
(*it)->update(ticks);
if(!((*it)->inView())) {
activeBullets.remove(*it);
Sprite* temp = *it;
it++;
inactiveBullets.push_back(temp);
} else {
it++;
}
}
}
void BulletFactory::更新(Uint32记号){
std::list::iterator it=activeBullets.begin();
while(it!=activeBullets.end()){
(*it)->更新(滴答声);
如果(!((*it)->inView()){
活动项目符号。删除(*它);
雪碧*温度=*它;
it++;
不活动子弹。推回(临时);
}否则{
it++;
}
}
}
当条件<代码>!((*it)->inView())正在执行true
存在分段错误。我看不出这个问题
编辑:忘记提到activeBullets和inactiveBullets是两个列表。您不能修改迭代器指向的元素,因为它会使迭代器无效。有关解决方案,请参阅。从这样的列表中删除是不安全的,您需要在新的迭代器工作时获得删除的结果。它可以工作,但是您可以更详细地解释这个问题吗。“删除”和“擦除”有什么区别。@user1198065:这个网站上有很多主题。你可以搜索它们。例如,请参见以下内容:
activeBullets.remove(*it);
Sprite* temp = *it; //<--- problem
it++; //<-- problem
Sprite* temp = *it;
it = activeBullets.erase(it); //use erase
//it++; don't increment it