Deleting from a sorted array with std::remove_if
In all our discussion of standard generic algorithms up to this point, we haven't covered the question of how to remove items from a range. This is because the concept of "a range" is fundamentally read-only: we might change the values of the elements of a given range, but we can never use a standard algorithm to shorten or lengthen the range itself. When, in the Shunting data with std::copy section, we used std::copy
to "insert into" a vector named dest
, it wasn't the std::copy
algorithm that was doing the inserting; it was the std::back_insert_iterator
object itself that held a reference to the underlying container and was able to insert into the container. std::copy
didn't take dest.begin()
and dest.end()
as parameters; instead it took the special object std::back_inserter(dest)
.
So how do we erase items from a range? Well, we can't. All we can do is erase items from a container; and the algorithms of the STL do not deal in containers. So...