the_drow
the_drow

Reputation: 19181

STL containers with fixed growth

I have a use case for containers that provide fixed growth patterns instead of exponential ones. In this use case, conserving memory is more important than runtime.
Is there a way to make std::vector or std::list to grow with N elements automatically instead of growing exponentially whenever the container runs out of space?
I know I can write an adapter that does this but I first want to find out if it's possible through standardized means.

If it's not possible, is there a boost container I can use?

Upvotes: 1

Views: 236

Answers (1)

DAle
DAle

Reputation: 9117

std::list does not grow exponentially, it's a doubly linked list under the hood.

std::vector cannot grow by a constant number of elements, because of complexities of operations required by the standard. For example, the complexity of push_back should have O(1) amortized time complexity. With the constant size grow it would be O(n) complexity.

If you need a container that is quite similar to std::vector and has no exponential growth factor, take a look at std::deque.

Upvotes: 4

Related Questions