std::basic_string::reserve

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
void reserve( size_type new_cap );

Increase the capacity of the string 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 and references, including the past-the-end iterator, are invalidated. Otherwise, no iterators or references are invalidated.

Contents

[edit] Parameters

new_cap - new capacity of the string

[edit] Return value

(none)

[edit] Complexity

At most linear in the size() of the string

[edit] Notes

reserve() cannot be used to reduce the capacity of the container; to that end shrink_to_fit() is provided.

[edit] Example

#include <cassert>
#include <string>
 
int main()
{
    std::string s;
    std::string::size_type new_capacity{ 100u };
    assert(new_capacity > s.capacity());
 
    s.reserve(new_capacity);
    assert(new_capacity <= s.capacity());
}


[edit] See also

returns the number of characters that can be held in currently allocated storage
(public member function)