Stl invalidating iterators

Perhaps this lack of attention to minimizing capacity isn’t surprising.

In most modern code, our concern is usually time performance rather than space requirements.

Of course, the iterators pointing to the removed elements gets invalidated, but all other iterators to the container remain valid. Note that for associative containers we don’t talk about “equal to a certain key” but rather “equivalent to a certain key”.

If you’re not familiar with it, this subtle difference is explained in details in Custom comparison, equality and equivalence with the STL.

If you have the key of the entry you want to remove from an associative container, it’s easy as pie: containers).

However, if you want to remove the elements of a map (or of its multi- of hash- counterparts) identified by their value and not their key, it’s not as straightforward.

Because deque doesn’t have a capacity() member function.In fact, the standard made it very unlikely that any implementation would reduce capacity with any call.The standard provides the guarantee that if reserve() is called, then any subsequence addition to the container will not invalidate iterators (until an addition increases the size of the container above the reserve() point.No because we can always expand the string to the “short” string limit without invalidating iterators (assuming the current size() is less than this limit).This means that if we currently have a string whose size() is less than the “short” string limit, a “request to reduce capacity() to size()” is going to fail.

Leave a Reply