October 2004
Intermediate to advanced
240 pages
6h 22m
English
Use a diet that works: To really shed excess capacity from a container, use the “swap trick.” To really erase elements from a container, use the erase-remove idiom.
Some containers (e.g., vector, string, deque) can end up carrying around extra capacity that’s no longer needed. Although the C++ standard library containers provide no guaranteed way to trim excess capacity, the following “swap trick” idiom works in practice to get rid of excess capacity for such a c of type container:
container<T>( c ).swap( c ); // the shrink-to-fit idiom to shed excess capacity
Or, to empty c out completely, clearing all contained elements and shedding all possible ...