I am not a user of custom allocator, but I was wondering whether a custom allocator can be used to redefine the growing strategy. For example, in most implementations, a std::vector
grows geometrically. Would it be possible to change this strategy for an arithmetic growth, like adding 10 elements everytime a reallocation is necessary. And if the answer if yes, then how to do that?
No. The reallocation strategy is defined by vector
, not by the allocator it uses. The allocator is there only to provide the memory - it is up to the vector
itself to tell it how much memory to provide.
The standard mandates that push_back
is amortized constant time. From [vector.modifiers]
Complexity: The complexity is linear in the number of elements inserted plus the distance to the end of the vector.
Since push_back
must be O(1)
, the growth strategy of vector
must not be arithmetic - otherwise we'd have linear-time insertion.
To do what you want, you'd have to provide your own vector
-like container and implement your own push_back()
member function.