I have a std::list
of Bananas
, and I want to get rid of the bad ones. Is there any relatively simple way to perform the following pseudocode?
foreach(Banana banana in bananaList)
{
if(banana.isBad()) bananaList.remove(banana);
}
(Making a transition from C# and Java to C++ has been a rocky road.)
bananaList.remove_if(std::mem_fun_ref(&Banana::isBad));
Note that you should probably be using std::vector
instead of std::list
though -- vector
performs better in 99.9% of cases, and it's easier to work with.
EDIT: If you were using vectors, vectors don't have a remove_if member function, so you'd have to use the plain remove_if
in namespace std
:
bananaVector.erase(
std::remove_if(bananaVector.begin(), bananaVector.end(), std::mem_fun_ref(&Banana::isBad)),
bananaVector.end());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With