c++ vectors erase check
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>=开发者_StackOverflowvRegularShots[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.
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++;
}
You really want to use the Erase-Remove Idiom to do this.
erase
doesn't fail. If you somehow pass the same parameters to erase
a second time, then it will try to delete something else. Depending on the exact scenario that might result in it deleting something you didn't intend to, or it might result in corrupting memory and possibly crashing the program if the parameters are no longer valid at all.
However, in the snippet of code you show, I don't see this being problem. Once any enemy is erased from the vector, there is no way the loop would ever encounter it again to even try testing another shot against it.
Though you do have a different problem: When an element is erased from a vector, the indexes of all subsequent elements in the vector are shifted downwards by 1. (If you have 4 elements in a vector, then they would be indexed 0,1,2,3. If you erase the element at index 1, then you'll have 3 elements left with indexes 0,1,2.) The consequence for the code you have is that when an element at indexs
is erased, another element will shift down to take that place. But the loop just continues and increments indexs
and so that an element will have been skipped.
It might happen that you erase several times from vRegularShots
using the same index
without checking that the index
is still in range
精彩评论