When does vector :: push_back increase capacity?
I use a bundle std::vectors
, setting them up at the beginning and using them push_back
to slowly fill them. Most of these vectors will be the same size (16 elements), although some may be larger. If I use push_back
16 times for a vector with size 0 and capacity 16 initially, can I be sure that the capacity will be exactly 16 after push_backs
?
source to share
Yes - if you reserve a certain capacity, the vector will not be reallocated until you exceed the set capacity 1 . Exactly how many more items you can click without reallocating isn't stated, but you're guaranteed, at least that's a lot.
- In particular, pointers and iterators to a vector are guaranteed to remain valid until you exceed the specified capacity.
source to share