Namespaces
Variants
Views
Actions

std::{{{1}}}::reserve

From cppreference.com
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.

Contents

Parameters

new_cap - new capacity of the container


Return value

(none)

Exceptions

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

See also

returns the number of elements that can be held in currently allocated storage
(public member function of std::{{{1}}}) [edit]
returns the maximum possible number of elements
(public member function of std::{{{1}}}) [edit]
changes the number of elements stored
(public member function of std::{{{1}}}) [edit]
reduces memory usage by freeing unused memory
(public member function of std::{{{1}}}) [edit]