1

I have a vector made up of objects from a class like this:

class Destroyable
{//.......
   public:
      bool isDestroyed();
//.........
};

How would I erase all of the elements that return true for isDestroyed()?

1 Answers1

2

You can use std::mem_fn to adapt a pointer-to-member-function as a function object with an explicit parameter of the class.

std::vector<Destroyable> vec = /* some values */
auto last = std::remove_if(vec.begin(), vec.end(), std::mem_fn(&Destroyable::isDestroyed));
vec.erase(last, vec.end());

In C++20 you can reduce the boilerplate

std::erase_if(vec, std::mem_fn(&Destroyable::isDestroyed));
Caleth
  • 52,200
  • 2
  • 44
  • 75