C++ std::string capacity() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::capacity() tells you how many characters the string\'s current buffer can hold without asking for a bigger block of memory.

Capacity is always >= size(). When you append past capacity, the string allocates a larger buffer (reallocation). Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Call capacity with empty brackets.

C++
std::size_t cap = s.capacity();
CallWhat it does
s.capacity()Returns the current buffer capacity in characters.

Small-string optimization (SSO) means short strings may live inside the object with a small fixed capacity. Exact numbers differ by library.

Library declarations
C++
size_type capacity() const noexcept;

Parameters

ParameterDescription
Nonecapacity() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
size_typeCharacters the current allocation can hold.

C++ Examples

Example 1 — Basic capacity()

Print size and capacity of a short string.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "Hi";
    std::cout << "size: " << s.size() << '\n';
    std::cout << "cap:  " << s.capacity() << '\n';
    return 0;
}

How It Works

  1. Size is 2 because the text is "Hi".
  2. Capacity is at least 2 and often larger due to SSO (exact value varies).
  3. The important idea: capacity is room; size is used length.

Example 2 — Capacity After reserve()

Ask for room, then check capacity.

C++
#include <iostream>
#include <string>

int main() {
    std::string s;
    std::cout << "before: " << s.capacity() << '\n';
    s.reserve(100);
    std::cout << "after:  " << s.capacity() << '\n';
    std::cout << "size:   " << s.size() << '\n';
    return 0;
}

How It Works

  1. An empty string still reports some small capacity under SSO.
  2. reserve(100) grows the buffer to at least 100.
  3. Size stays 0 because no characters were added.

Example 3 — Capacity Can Jump on Append

Append until capacity changes.

C++
#include <iostream>
#include <string>

int main() {
    std::string s;
    s.reserve(4);
    std::size_t cap = s.capacity();
    std::cout << "start cap: " << cap << '\n';

    while (s.size() <= cap) {
        s.push_back('x');
    }
    std::cout << "size: " << s.size() << '\n';
    std::cout << "new cap: " << s.capacity() << '\n';
    return 0;
}

How It Works

  1. We reserve at least 4 characters of room.
  2. Pushing characters until size passes the old capacity forces a reallocation.
  3. The new capacity is larger (exact growth factor varies by library).

Frequently Asked Questions

How many characters the current buffer can hold without reallocating.
No. Capacity is at least size, and often larger after reserve or growth.
No. It only reports the current buffer size.
Appending past capacity reallocates. Old iterators and references become invalid.
Call reserve(n) before a large number of appends.

Next: cbegin()

Keep going through the string functions. const_iterator to the first character (read-only).

cbegin() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful