std::{{{1}}}::reserve
void reserve( size_type new_cap ); |
(since {std}) | |
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.
Information on iterator invalidation is copied from here |
Contents |
Parameters
new_cap | - | new capacity of the container
|
Return value
(none)
Exceptions
- std::length_error if new_cap > max_size().
- any exception thrown by
Allocator::allocate()
(typically std::bad_alloc)
If an exception is thrown, this function has no effect (strong exception guarantee).
If T 's move constructor is not noexcept and T is not CopyInsertable into *this , vector will use the throwing move constructor. If it throws, the guarantee is waived and the effects are unspecified. |
(since C++11) |
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.
Example
This section is incomplete Reason: no example |
See also
returns the number of elements that can be held in currently allocated storage (public member function of std::{{{1}}} )
| |
returns the maximum possible number of elements (public member function of std::{{{1}}} )
| |
changes the number of elements stored (public member function of std::{{{1}}} )
| |
reduces memory usage by freeing unused memory (public member function of std::{{{1}}} )
|