C++ std::string size() Function

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

Definition

In C++, std::string::size() returns how many characters are stored in the string. Counting starts at 0 for an empty string.

For std::string, size() and length() return the same value. Prefer either name consistently. 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 size with empty brackets.

C++
std::size_t n = s.size();
std::cout << n;
CallWhat it does
s.size()Returns the current character count as std::size_t.

size() does not count a C-string terminator inside the std::string object. c_str() adds a '\\0' for C APIs, but that extra byte is not part of size().

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

Parameters

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

Return Value

Return ValueDescription
size_typeUsually std::size_t. The number of characters currently stored.

C++ Examples

Example 1 — Basic size()

Print the size of a greeting.

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

int main() {
    std::string hi = "Hello";
    std::cout << "size: " << hi.size() << '\n';
    return 0;
}

How It Works

  1. "Hello" has five letters.
  2. size() returns 5.
  3. No null terminator is included in that count.

Example 2 — Size Grows With Text

Watch size change when you append characters.

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

int main() {
    std::string s = "C++";
    std::cout << s.size() << '\n';
    s += " string";
    std::cout << s.size() << '\n';
    s.push_back('!');
    std::cout << s.size() << '\n';
    return 0;
}

How It Works

  1. The starting text "C++" has size 3.
  2. Appending " string" adds seven characters, so size becomes 10.
  3. push_back('!') adds one more character for size 11.

Example 3 — size() Versus capacity()

Print both after reserve to see the difference.

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

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

How It Works

  1. reserve(32) asks for room for at least 32 characters.
  2. Assigning "hi" sets the used length to 2.
  3. Capacity stays at least 32, so size and capacity differ.

Frequently Asked Questions

The number of characters currently stored in the string, as size_type (usually size_t).
Yes for std::string. Both return the character count. Pick one style and stay consistent.
No. The std::string length excludes the extra '\0' that c_str() exposes for C functions.
size() is how many characters you store. capacity() is how many the current buffer can hold without reallocating.
Yes. std::string keeps its length, so size() is constant time.

Next: starts_with()

Keep going through the string functions. Test whether the string begins with a prefix (C++20).

starts_with() 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