Removing items from a collection in the STL requires a technique used so often that is has become an idiom: the erase-remove-idiom
One of the most common usages of this idiom is to remove an item of type T
from a vector<T>
std::vector<Widget> widget_collection;
Widget widget;
widget_collection.erase(
std::remove(widget_collection.begin(), widget_collection.end(), widget),
widget_collection.end());
This is obviously very verbose, and violates the DRY principle - the vector in question is required 4 times there.
So my question is why does the standard not provide a convenience helper?
Something like
widget_collection.erase_remove(widget);
or
std::erase_remove(widget_collection, widget);
This could obviously be extended to
widget_collection.erase_remove_if(widget, pred);
etc...
<algorithm>
so all could benefit from it – Oberhausenlist<>
? – Holleylist::erase
. It could certainly be made more generic. I'm gladN4273
is accepted as it is definitely more elegant and has wider coverage than my implementation! – Oberhausen