Skip to content
Tutorials Center
My learning

Topics

C++ Reference Β· Strings and I/O

C++ string::size, length and capacity

Understand size, length, capacity, reserve, shrink_to_fit, resize and max_size of std::string, small string optimization, and UTF-8 byte versus character counts.

A std::string tracks three different numbers. The size (also called length, same value) is how many characters it currently holds, not counting the hidden terminating null. The capacity is how many characters it can hold before it must allocate a bigger buffer. And max_size() is the theoretical upper limit of the implementation.

size() and length() are synonyms; size() matches other containers and is the better choice in generic code. Both are constant time because the size is stored, not computed by scanning for a null like C's strlen.

C++
std::string s = "hello";
s.size();       // 5
s.length();     // 5
s.capacity();   // >= 5 (typically 15 for small strings in libstdc++)
s.empty();      // false

Try it

C++ Size, capacity and growth

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Bytes are not characters

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
size_type size() const noexcept;
size_type length() const noexcept;
size_type max_size() const noexcept;
size_type capacity() const noexcept;
bool empty() const noexcept;
void reserve(size_type new_cap);
void shrink_to_fit();
void resize(size_type count);
void resize(size_type count, char ch);
template<class Op> void resize_and_overwrite(size_type count, Op op);   // C++23
Name Description
new_cap Minimum capacity wanted. A value not above the current capacity does nothing in C++20 and later (before C++20 it was a non-binding shrink request).
count New size for resize.
ch Fill character for new positions; the default is '\0'.
op Callable (char* buffer, size_type count) -> size_type that fills the buffer and returns the real size (C++23).

Returns: the sizes as std::size_t-like unsigned values; empty() returns a bool.

Errors: std::length_error if a requested size exceeds max_size().

Usage notes

  • Unsigned arithmetic. s.size() - 1 on an empty string wraps around to a huge number. Test empty() or write i + 1 < s.size().
  • Bytes, not characters. size() counts char units. A UTF-8 string with accents or emoji has more bytes than visible characters.
  • Small string optimization. Short strings (about 15 chars in libstdc++, 22 in libc++) live inside the object with no heap allocation; their capacity is that internal size. The exact threshold is implementation-defined.
  • reserve helps loops. Call it once when you can estimate the final length. It never changes size().
  • shrink_to_fit is a non-binding request and may be ignored; since C++20, reserve never shrinks.
  • resize changes the content; reserve does not. Writing past size() (even within capacity()) is undefined behavior.
  • Reallocation invalidates iterators, pointers and references to the characters.
  • Fast filling: C++23 resize_and_overwrite avoids zeroing the new bytes before a C API writes into them.