std::vector::reserve
void reserve( size_type new_cap ); |
||
Increase the capacity of the container to a value that's greater or equal to new_cap
. If new_cap
is greater than the current capacity(), new storage is allocated, otherwise the method does nothing.
If new_cap
is greater than capacity(), all iterators, including the past-the-end iterator, and all references to the elements are invalidated. Otherwise, no iterators or references are invalidated.
Parameters
new_cap | - | new capacity of the container |
Type requirements | ||
-T must meet the requirements of MoveInsertable .
|
Return value
(none)
Exceptions
- std::length_error if new_cap > max_size().
- any exception thrown by
Allocator::allocate()
(typically std::bad_alloc)
Complexity
At most linear in the size() of the container.
Notes
reserve() cannot be used to reduce the capacity of the container, to that end shrink_to_fit() is provided.
Correctly using reserve() can prevent unnecessary reallocations, but inappropriate uses of reserve() (for instance, calling it before every push_back() call) may actually increase the number of reallocations (by causing the capacity to grow linearly rather than exponentially) and result in increased computational complexity and decreased performance.