Consistent Container Erasure
Understand the details of 'erase' and 'erase_if' in C++20.
We'll cover the following...
We'll cover the following...
Before C++20, removing elements from a container was too complicated. Let me show you why.
The erase-remove idiom
Removing an element from a container seems to be quite easy. In the case of a std::vector you can use the function std::remove_if.
The program above removes all elements from the std::vector that are less than zero. Easy, right? Maybe not; now, you fall into the trap that is well-known to many seasoned C++ programmers.
std::remove_if (line 13) does not remove anything. The std::vector still has the same number of elements. Both algorithms return the new logical end of the modified container.
To modify a container, you have to apply the ...