Lag when removing bodies.

I've implemented coins in my game, and to keep things consistent and to simplify collision detection, each coin is represented by a Box2D body. When there is a collision between the player and a coin, a flag in the coin object is set to true, which tells the game class that it should be removed. After calling world->Step(...), I remove the bodies via this line of code:
Code:
entities_.erase(std::remove_if(entities_.begin(), entities_.end(), [](std::unique_ptr<Entity>& ent) { return ent->shouldRemove(); }), std::end(entities_));


The coins are being removed fine, but there's a huge amount of lag. Even when just collecting one coin (so just one body being removed), it lags horribly.

If anyone could help me with this it'd be much appreciated!
Thanks guys.
remove_if and erase are both linear complexity, so you would need a colossal number of elements before they would cause any noticeable slowdown.

Does it happen in both release and debug builds? What compiler/debugger are you using? I had never heard of unique_ptr so I did some reading to familiarize myself with it and came across this, maybe it's relevant: http://stackoverflow.com/questions/1068 ... ached-msvc

You could try profiling to see where the time is going.

Original thread: https://box2d.org/forum/viewtopic.php?t=9621