Killing AI

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

actually your code doesn't work. It doesn't actually swap the objects, it just deletes the last one. I'm in need of a solution for your code

Advertisement

Do you have included <memory> header? It is necessary for shared_ptr.

What compiler are you using?

If you want "pure" pointers then code is a bit different:

class CZombie { ...
}; typedef std::vector<CZombie*> v_Zombies class CZombieMgr { v_Zombies zombies; public: ... ~CZombieMgr() { for(std::size_t i = 0; i < zombies.size(); ++i) { delete zombies;
}
} void addZombie(int h) { zombies.push_back( new CZombie(h) );
} void removeDeadZombies() { for(std::size_t i = 0; i < zombies.size(); ++i) { if(zombies->isDead()) { std::swap( zombies[i--], zombies.back() ); delete zombies.back(); zombies.pop_back(); }
}
}; ...

@moderators

Sorry for double post, but if i go to EDIT then it somehow messes my code up.

@mypal1600000

Can you post relevant bits from your code? it will be easier to help you.

No! It won't be needed as my code has been fixed. Thanks to all of you who, by discussing together, have found the solution to the problem.

Without iterators:


for(std::size_t i = 0; i < v.size(); ++i)
	{
		if(v[i].dead)
		{
			
			std::swap(v[i], v.back());
			v.pop_back();
			--i;
		}
	}

Remember that modification of the for-loop index inside the loop is one bad programming practice. It usually leads to infinite loops and computer hang ups.

This topic is closed to new replies.

Advertisement