4 ms·
It seems that even vector::shrink_to_fit is implemented that way. That is, when you call shrink_to_fit, it first copies all elements it has, swaps itself with
by yongjik 13y ago
It seems that even vector::shrink_to_fit is implemented that way.
That is, when you call shrink_to_fit, it first copies all elements it has, swaps itself with the new copy, and deletes the original vector. (Probably because there's no portable way of returning only part of a contiguous memory region. Yikes.)
http://stackoverflow.com/questions/2695552/how-to-shrink-to-fit-an-stdvector-in-a-memory-efficient-way http://stackoverflow.com/questions/2695552/how-to-shrink-to-...
- dbaupp 13y agoI think the reason is the existence of move/copy constructors, i.e. it's invalid to just move a C++ object in memory (as a realloc would do), in general. (shrink_to_fit does need to (attempt to) get a smaller allocation, otherwise or wouldn't be shrinking the vector.)