Despite their names, the standard remove algorithms (std::remove
, std::remove_if
, std::unique
) do not erase
elements from a given range. Instead, they shift the preserved (not removed) elements to the beginning of the range and return an iterator after the
last preserved element. The "removed" elements have unspecified values.
C++20 introduced functions in the std::ranges
namespace with the same names. Aside from returning a subrange instead of an iterator,
they exhibit the same behavior.
Ignoring the result of any of these functions indicates a bug: It is impossible to distinguish removed elements in the container from the others.
As a result, any further operations on the container may access elements with unspecified values. And this may lead to invalid program states, data
corruption, or crashes.