Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

resize vs reserve in std::vector and std::basic_string

Use `resize` to change how many elements exist. Use `reserve` to make room for future elements without changing the current size or contents.

Version: ISO C++23; implementation support may vary

Syntax

// Selected ISO C++23 member declarations, grouped by owning class.
// std::vector<T, Allocator> — <vector>
constexpr void reserve(size_type n);
constexpr void resize(size_type n);
constexpr void resize(size_type n, const T& value);

// std::basic_string<CharT, Traits, Allocator> — <string>
constexpr void reserve(size_type n);
constexpr void resize(size_type n);
constexpr void resize(size_type n, CharT ch);

In C++23, both std::vector and std::basic_string distinguish size (size()) from capacity (capacity()). reserve(n) leaves the size and contents unchanged. If n > capacity(), it reallocates and the new capacity is at least n; otherwise capacity stays the same. Reserved space does not extend the range of elements you can access. The no-argument std::basic_string::reserve() overload is deprecated in C++23; std::vector has no such overload.

After a successful resize(n) or resize(n, value), the size is exactly n. Shrinking removes trailing elements, and requesting the current size leaves the contents alone. When growing, std::vector::resize(n) appends default-inserted elements according to the allocator's rules (zeros for std::vector<int> with its standard allocator). The two-argument overload appends copies of value without replacing existing elements.

For std::basic_string, the one-argument overload appends CharT() values ('\0' for std::string), and the two-argument overload appends the supplied character. Appended nulls count toward size(), but functions expecting a C-style string stop at the first null. In UTF-8 text, std::string counts bytes, not user-visible characters; truncation can split a multibyte sequence.

Vector reallocation invalidates all pointers, references, and iterators to its elements, as well as the old end(). Do not use handles to removed elements. A string's resize can invalidate handles even if its capacity stays the same. Shrinking the size is not a way to guarantee that storage is released. For both types, reserve(n) throws std::length_error if n > max_size(); so does a string's resize(n). Allocation or construction of vector elements can also throw.

Examples

#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<int> v{10, 20};
    std::string s = "ab";

    v.reserve(8);
    s.reserve(8);
    std::cout << "reserve: " << v.size() << ' ' << s.size() << '\n';

    v.resize(3);
    s.resize(3);
    std::cout << "default: " << v.back() << ' ' << (s.back() == '\0') << '\n';

    v.resize(5, 7);
    s.resize(5, '*');
    std::cout << "fill: " << v.back() << ' ' << s.back() << '\n';

    v.resize(1);
    s.resize(1);
    std::cout << "shrink: " << v.size() << ' ' << s.size()
              << " | " << v.front() << ' ' << s << '\n';
}

Result

reserve: 2 2
default: 0 1
fill: 7 *
shrink: 1 1 | 10 a

After reservation, both sizes are still 2. On the `default` line, `1` means the last character is `'\0'`. The next lines show explicit fill values and truncation to one element.

Related lessons

  • C++ · Lesson 5 · Advanced · Version: 3

Sources