Reputation: 95
I currently have a zombies project that I've been given for uni but I'm stuck on a couple of things.
How to properly remove a vector? We have zombies and pill objects that are stored in vectors. My first thought was to change the symbol but obviously that doesn't work since that's all I was changing like this:
for (int i = 0; i < MAXPILLS; i++)
{
//if pill coordinates = spot coordinates
if ((pills.at(i).y == sp.y) && (pills.at(i).x == sp.x))
{
pills.at(i).symbol = TUNNEL; //make pill a tunnel
}
}
This pill object needs to be completely removed instead. I have to do this also for when a zombie falls down a hole (sp
is an instance of the zombie class).
How can I remove it all together?
Upvotes: 0
Views: 94
Reputation: 1352
Use remove_if algorithm instead:
pills.erase(std::remove_if(pills.begin(), pills.end(), [&sp](const decltype(pills.at(0)) &p){return p.x == sp.x && p.y == sp.y;}));
Note that I'm using lambda function here which is C++11 construct so you might want to check whether you're allowed to use that in your assignment first and replace it with functor object if you're not allowed to use C++11.
Upvotes: 2