non-method remove() can’t shrink a container

Suppose 5 items are to be removed and 10 items to remain. Scott Meyers said something to the effect that “remove() will put the 10 items in the front-segment, and return an iterator to indicate end of the front-segment, but the container size will remain 15.

Now, to shrink the container, you need to get a pointer to the container not its elements. With the pointer, either a method or a function accepting the ptr can shrink the container.

Function remove() gets no such ptr. It only gets iterators. Iterators provide access to the elements, not the container.

An STL container has pointers to the data structure (almost always on heap), but the data structure doesn’t have pointer to the host container, which can be on stack or global memory.

留下评论