C++ std::string resize_and_overwrite() Function

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

Definition

In C++, std::string::resize_and_overwrite() prepares a buffer of size up to n and lets a function write directly into it (C++23).

The operation returns how many characters were finalized as the new size. 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 a max size and a callable.

C++
s.resize_and_overwrite(8, [](char* p, std::size_t n) {
    p[0] = 'O'; p[1] = 'K';
    return std::size_t{2};
});
CallWhat it does
s.resize_and_overwrite(n, op)Allow op to write up to n chars; return the final length.

Designed for efficient bulk writes without temporary strings. Needs C++23.

Library declarations
C++
template<class Operation>
constexpr void resize_and_overwrite(size_type n, Operation op); // C++23

Parameters

ParameterDescription
nMaximum number of characters the operation may write.
opCallable taking (char* p, size_type n) and returning the final size used.

Return Value

Return ValueDescription
voidFinal size is set from the value returned by op.

C++ Examples

Example 1 — Basic resize_and_overwrite

Write OK into the buffer.

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

int main() {
    std::string s = "old";
    s.resize_and_overwrite(4, [](char* p, std::size_t n) {
        (void)n;
        p[0] = 'O';
        p[1] = 'K';
        return std::size_t{2};
    });
    std::cout << s << '\\n';
    return 0;
}

How It Works

  1. Old contents are overwritten via the callback.
  2. We write two characters and return 2.
  3. The string size becomes 2 with text "OK".

Example 2 — Write a Pattern

Fill with ascending digits.

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

int main() {
    std::string s;
    s.resize_and_overwrite(3, [](char* p, std::size_t n) {
        for (std::size_t i = 0; i < n; ++i) {
            p[i] = static_cast<char>('1' + i);
        }
        return n;
    });
    std::cout << s << '\\n';
    return 0;
}

How It Works

  1. The operation may use all n slots.
  2. Digits 1,2,3 are written.
  3. Returning n keeps size 3.

Example 3 — Return Smaller Size

Allocate room for 5 but keep 1.

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

int main() {
    std::string s;
    s.resize_and_overwrite(5, [](char* p, std::size_t) {
        p[0] = 'Z';
        return std::size_t{1};
    });
    std::cout << s << ' ' << s.size() << '\\n';
    return 0;
}

How It Works

  1. Buffer space for 5 is prepared.
  2. Only one character is committed.
  3. Final size is 1.

Frequently Asked Questions

C++23 API that lets you write directly into a resized string buffer through a callback.
C++23.
The number of characters that should become the new size, <= n.
Avoid temporary buffers when filling a string from C APIs or custom writers.
resize then write via data()/indexing, or build in a temporary buffer and assign.

Next: rfind()

Keep going through the string functions. Find the last occurrence of a substring or character.

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

12 people found this page helpful