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

?

+3


source to share


3 answers


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.
+7


source


23.3.6.5 [vector modifiers]

void push_back(const T& x);
void push_back(T&& x);

      



Notes: causes reallocation if new size is larger than old capacity

Quite a lot of explanations.

+2


source


Yes.

23.3.6.3p6:

During post-invocation inserts, reserve()

no reallocation occurs until the insertion makes the vector size larger than the value capacity()

.

+2


source







All Articles