We can erase one element/ entry from a container by the popular erase–remove idiom. However, many of us would have encountered some problems while applying this idiom:
one can easily get into the pitfall of typos like
c.erase(std::remove_if(c.begin(), c.end(), pred)); // , c.end() //---> missing hereor
c.erase((std::remove_if(c.begin(), c.end(), pred), c.end())) // ^^ ^^ // extra () makes it pass only c.end() to the c.erase- It even follows the wrong semantics for containers like
std::listby not selecting its own memberstd::list::remove_if()for the idiom. - Thirdly, using
std::remove_ifdoes not work for associative containers.
Do we have anything generalized and less typo-prone than std::erase-std::remove_if or something like std::erase_if within the scope of c++17, or will there be such a utility in c++20?