C++ std::string resize() Function

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

Definition

In C++, std::string::resize() sets the string to a new length.

Growing pads with '\\0' (or a fill char). Shrinking cuts the end. 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

Pass the new size, and optionally a fill character.

C++
s.resize(10);
s.resize(10, '*');
CallWhat it does
s.resize(n)Set size to n; new chars are char() which is a null character.
s.resize(n, ch)Set size to n; new chars are ch.

Default fill is a null character, which can look invisible when printing. Prefer an explicit fill when you want visible padding.

Library declarations
C++
void resize(size_type n);
void resize(size_type n, char c);

Parameters

ParameterDescription
nNew size.
cOptional fill character used when growing.

Return Value

Return ValueDescription
voidNo return value.

C++ Examples

Example 1 — Resize Larger

Pad with stars.

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

int main() {
    std::string s = "hi";
    s.resize(5, '*');
    std::cout << s << '\\n';
    return 0;
}

How It Works

  1. Size was 2.
  2. Resize to 5 adds three '*'.
  3. Result is "hi***".

Example 2 — Resize Smaller

Truncate the end.

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

int main() {
    std::string s = "abcdef";
    s.resize(3);
    std::cout << s << '\\n';
    return 0;
}

How It Works

  1. New size 3 keeps the first three characters.
  2. Trailing "def" is discarded.
  3. Capacity may still be larger.

Example 3 — Resize Empty

resize(0) clears characters.

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

int main() {
    std::string s = "data";
    s.resize(0);
    std::cout << std::boolalpha << s.empty() << '\\n';
    return 0;
}

How It Works

  1. Size becomes 0.
  2. No characters remain.
  3. Similar end state to clear for the character count.

Frequently Asked Questions

It changes the string length, padding or truncating as needed.
char() which is the null character \0.
Usually no. Use shrink_to_fit to request that.
No. reserve changes capacity only; resize changes size (visible length).
C++23 API to resize while writing into the buffer through a callback.

Next: resize_and_overwrite()

Keep going through the string functions. Resize and let a callback write the new characters (C++23).

resize_and_overwrite() 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.

11 people found this page helpful