"Why don’t algorithms call
erase()
by themselves? This question highlights the price of the flexibility of the STL. The STL separates data structures and algorithms by using iterators as the interface.However, iterators are an abstraction to represent a position in a container. In general, iterators do
not
know their containers. Thus, the algorithms, which use the iterators to access the elements of the container, can’t call any member function for it.
This design has important consequences because it allows algorithms to operate on ranges that are different from “all elements of a container.” For example, the range might be a subset of all elements of a collection. The range might even be a container that provides no erase() member function (an array is an example of such a container). So, to make algorithms as flexible as possible, there are good reasons not to require that iterators know their container.
This design has important consequences because it allows algorithms to operate on ranges that are different from “all elements of a container.” For example, the range might be a subset of all elements of a collection. The range might even be a container that provides no erase() member function (an array is an example of such a container). So, to make algorithms as flexible as possible, there are good reasons not to require that iterators know their container.
Note that often it is not necessary to remove the “removed” elements. Often, it is no problem to use the returned new logical end instead of the real end of the container. In particular, you can call all algorithms with the new logical end. "
---<<The C++ Standard Library A Tutorial and Reference (2nd Edition)>>