By reading the std::vector reference I understood that
-
calling
insertwhen the the maximum capacity is reached will cause the reallocation of thestd::vector(causing iterator invalidation) because new memory is allocated for it with a bigger capacity. The goal is to keep the guarantee about contiguous data. -
As long as I stick below the maximum capacity
insertwill not cause that (and iterators will be intact).
My question is the following:
When reserve is called automatically by insert, is there any way to control how much new memory must be reserved?
Suppose that I have a vector with an initial capacity of 100 and, when the maximum capacity is hit, I want to allocate an extra 20 bytes.
Is it possible to do that?
You can always track it yourself and call reserve before it would allocate, e.g.
You can wrap this in a function of your own and call that function instead of calling
insert()directly.