Killing AI

Started by
33 comments, last by Mercile55 11 years, 3 months ago

Could you please describe what steps you've taken based on the information you've been given so far?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement


for (vector<zombiesClass*>::iterator i = zombies.begin() ; 
i != zombies.end() ;)
{
if( (*i)->Dead )
{
delete zombies(i);
zombies.erase(i);
}
else ++i;
}

Perhaps something like this? (argh, cant get rid of italic text..)

Im not to sure this works thou. It deallocated the object and removes it from the vector without invalidizing the vector. Atleast that's what I intent to do with this code.

If you don't understand the stuff written here, please sharpen your C++ skills.

|error: 'zombieClass' cannot appear in a constant-expression|

for (vector<zombiesClass*>::iterator i = zombies.begin() ; 
i != zombies.end() ;)
{
if( (*i)->Dead )
{
delete zombies(i);
zombies.erase(i);
}
else ++i;
}

Perhaps something like this? (argh, cant get rid of italic text..)

Im not to sure this works thou. It deallocated the object and removes it from the vector without invalidizing the vector. Atleast that's what I intent to do with this code.

Closer, but incorrect for... several reasons. At this point I'm thinking that handing him working code would set a bad precedent, though.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Any suggestions?

Try using a debugger to see what actually happens.

Are you declaring your vector like this:
std::vector<Zombie> zombies;

Or like this:
std::vector<Zombie*> zombies;

P.S: I have tried simple deleting the object using delete zombies[ i ], but the program just stops responding

You can only 'delete' objects you have 'new'd. If you don't know what new or delete do, just don't touch them at all until you thoroughly research the issue.
new and delete are dangerous to your code (but won't do any harm to your computer, so feel free to experiment) if used improperly.

std::vectors are made for very fast access. It's not good for fast removals or fast inserts, unless you remove or insert from the end of the list with push_back() or pop_back(). You probably want a std::list, which is designed for inserting and erasing from in the very middle of the container.
std::list<Zombie> zombies;
typedef std::list<Zombie>::iterator ZombieIterator;
for(ZombieIterator it = zombies.begin(); it !- zombies.end(); it++)
{
	if(it->dead)
	{
		it = zombies.erase(it); //erase() removes the element at the position given, and returns the next element.
		it--; //Take a step back, so the for-loops 'it++' doesn't skip an element.
	}
}

Alternatively, you could use the "swap and pop" method. Swaps are fast. Pops are fast. You swap the dead zombie with the live zombie at the end of the vector (messing up ordering, but you might not care about the order in this situation), and pop back the vector erasing the dead zombie.
std::vector<Zombie> zombies;
typedef d::vector<Zombie>::iterator ZombieIterator;

for(ZombieIterator it = zombies.begin(); it != zombies.end(); it++)
{
	if(it->dead)
	{
		//Swap the value of the current zombie with the zombie at the back of the container.
		std::swap(*it, zombies.back());
		//Pop the back of the container.
		zombies.pop_back();
	}
}

A third method is to use a standard algorithm like std::remove_if() to do this for you. You pass a range of iterators to std::remove_if(), and you also pass a callback function or functor to say which elements to remove.
bool ZombieIsDead(const Zombie &zombie)
{
	return zombie.dead;
}

std::vector<Zombie> zombies;
typedef d::vector<Zombie>::iterator ZombieIterator;

ZombieIterator newEnd = std::remove_if(zombies.begin(), zombies.end(), ZombieIsDead);
zombies.erase(newEnd, zombies.end());

If using C++11, you can use a lambda as a callback function.

Two of those methods are broken.

In the first method, if you erase the first element, the iterator will be pointing to the first element still when it is decremented, which is undefined behaviour.

In the second method, if you erase the last element, the iterator will be pointing at the end when it is incremented, which undefined behaviour again (and probably a crash).

The correct method would be to move the ++it inside the body of the for loop, and only call it if erase was not called, otherwise set the iterator to the return value of erase.

I knew I should've tested the snippets before posting them. laugh.png

So the correct method to 'swap and pop' is:


std::vector<Zombie> zombies;
typedef d::vector<Zombie>::iterator ZombieIterator;
 
for(ZombieIterator it = zombies.begin(); it != zombies.end();)
{
	if(it->dead)
	{
		//Swap the value of the current zombie with the zombie at the back of the container.
		std::swap(*it, zombies.back());
		//Pop the back of the container.
		zombies.pop_back();
	}
	else
	{
		++it;
	}
}

Looking over my previous version, it'd also skip whatever elements were at the end before being swapped. rolleyes.gif

But the correct choice in most cases is the third solution I posted, using the heavily optimized and standardized std::remove_if() algorithm.

I knew I should've tested the snippets before posting them. laugh.png

So the correct method to 'swap and pop' is:
std::vector<Zombie> zombies;typedef d::vector<Zombie>::iterator ZombieIterator; for(ZombieIterator it = zombies.begin(); it != zombies.end();){	if(it->dead)	{		//Swap the value of the current zombie with the zombie at the back of the container.		std::swap(*it, zombies.back());		//Pop the back of the container.		zombies.pop_back();	}	else	{		++it;	}}
Looking over my previous version, it'd also skip whatever elements were at the end before being swapped. rolleyes.gif

But the correct choice in most cases is the third solution I posted, using the heavily optimized and standardized std::remove_if() algorithm.
This isn't right man, try this when all Zombies are dead, and you'll end up with a crash, as when you pop_back the very first element, the iterator becomes invalid. It will be ok, as long as there's at least one element left though.
It's always good idea to make sure what does and what doesn't invalidate your iterator, and avoid any such cases if possible. It saves time in the future.

remove_if() does the trick for most cases, although it may not be as fast, due to additional calls overhead.

This topic is closed to new replies.

Advertisement