Is there a way in C++ to check that erase succeeds?
I have two pieces of code that erase the same object. The first erased the object, then the second tries to erase it but doesn't find the object.
Any ideas?
for(long indexs=0; indexs < (long)Enemie1.vS2Enemie1.size(); indexs++)
{
if((vRegularShots[index].x>=Enemie1.vS2Enemie1[indexs].x && vRegularShots[index].y>=Enemie1.vS2Enemie1[indexs].y && vRegularShots[index].y<=(Enemie1.vS2Enemie1[indexs].y+17) && vRegularShots[index].x<=(Enemie1.vS2Enemie1[indexs].x+17))||(Enemie1.vS2Enemie1[indexs].x>=vRegularShots[index].x && Enemie1.vS2Enemie1[indexs].y>=vRegularShots[index].y && Enemie1.vS2Enemie1[indexs].y<=(vRegularShots[index].y+16) && Enemie1.vS2Enemie1[indexs].x<=(vRegularShots[index].x+5)))
{
Enemie1.vS2Enemie1.erase(Enemie1.vS2Enemie1.begin()+indexs);
vRegularShots.erase(vRegularShots.begin()+index);
score+=100;
}
}
vregularshots
holds the shots that were launched and vS2Enemie1
holds the enemies. if there were two shots that touched the enemy in the same time, then it will loop through the shots and check if it touched any enemies, then erase the enemy and the shot. But when another shot also touched the enemy, it will try to erase the enemy that was already erased.
Any idea ?
UPDATE
for(long indexs=0; indexs < (long)Enemie1.vS1Enemie1.size();)
{
if((vRegularShots[index].x>=Enemie1.vS1Enemie1[indexs].x && vRegularShots[index].y>=Enemie1.vS1Enemie1[indexs].y && vRegularShots[index].y<=(Enemie1.vS1Enemie1[indexs].y+17) && vRegularShots[index].x<=(Enemie1.vS1Enemie1[indexs].x+17))||(Enemie1.vS1Enemie1[indexs].x>=vRegularShots[index].x && Enemie1.vS1Enemie1[indexs].y>=vRegularShots[index].y && Enemie1.vS1Enemie1[indexs].y<=(vRegularShots[index].y+16) && Enemie1.vS1Enemie1[indexs].x<=(vRegularShots[index].x+5)))
{
Enemie1.vS1Enemie1.erase(Enemie1.vS1Enemie1.begin()+indexs);
vRegularShots.erase(vRegularShots.begin()+index);
score+=100;
}
else
indexs++;
}