Namespaces
Variants
Views
Actions

Talk:cpp/container/vector/shrink to fit

From cppreference.com

The complexity is bad. It is not constant, but linear.

23.3.6.3:

   void shrink_to_fit();
   Requires: T shall be MoveInsertable into *this.
   Complexity: Linear in the size of the sequence.
You are not quoting the C++11 standard document, you're quoting the C++14 working draft. This particular change (type requirement and complexity for shrink_to_fit) were part of the resolution to the library issue #2033 --Cubbi 03:27, 13 May 2013 (PDT)

[edit] Pointer invalidation

The note about invalidation should talk about references, *pointers* and iterators. I'm not familiar enough with the wiki markup to do the change myself.

there is nothing special about pointers: they are invalidated when (because) the references are. The standard does mention pointers when discussing vector::shrink_to_fit, but in many other situations it only talks about iterators and references: associative.reqmts/9, unord.req/14, deque.modifiers/1, deque.modifiers/4, forwardlist.modifiers/1, forwardlist.ops/12, forwardlist.ops/16 (I stopped grepping at this point). Are pointers really worth the effort to list separately? --Cubbi (talk) 05:58, 5 July 2017 (PDT)

[edit] Iterator Invalidation section is wrong, I can't figure out how to edit

Currently there's a paragraph that says: If after the operation the new size() is greater than old capacity() a reallocation takes place, in which case all iterators (including the end() iterator) and all references to the elements are invalidated. If no reallocation occurs, no iterators or references are invalidated.

This method should not change size(), so size() should not be new. Also the capacity should shrink, not grow, so the size exceeding the old capacity would be a problem. A possible correction would be:

If after the operation the capacity() has changed a reallocation takes place, in which case all iterators (including the end() iterator) and all references to the elements are invalidated. If no reallocation occurs, no iterators or references are invalidated.

fixed it up to not mention "new size", thanks --Cubbi (talk) 11:16, 15 September 2023 (PDT)