Amortized Analysis

Martin Sebor sebor@roguewave.com
Sat Jul 7 15:38:00 GMT 2001


Kevin Atkinson wrote:
> 
...
> 
> Personally I think all STL containers should have a shrink method so that
> the container can shrink to the exact size needed once you are done adding
> to it.  I know you can always create a new container and make a copy but
> this has the potential to be not as efficient as just freeing the unused
> memory.  Because in some cases this memory can be freed without any
> copying or reallocation being done.  For example in the case of very large
> strings which are allocated with mmap.

reserve() can be used to release unused space, although it's
an implementation detail whether reserve() actually releases
any memory or not.

Regards
Martin



More information about the Libstdc++ mailing list