Home C++ Strings Functions resize_and_overwrite() C++ std::string resize_and_overwrite() Function 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.
s.resize_and_overwrite(8, [](char* p, std::size_t n) {
p[0] = 'O'; p[1] = 'K';
return std::size_t{2};
}); Call What 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 template<class Operation>
constexpr void resize_and_overwrite(size_type n, Operation op); // C++23 Parameters Parameter Description nMaximum number of characters the operation may write. opCallable taking (char* p, size_type n) and returning the final size used.
Return Value Return Value Description voidFinal size is set from the value returned by op.
C++ Examples 1. Write two chars 2. Fill pattern 3. Use fewer Example 1 — Basic resize_and_overwrite Write OK into the buffer.
#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 Old contents are overwritten via the callback. We write two characters and return 2. The string size becomes 2 with text "OK". Example 2 — Write a Pattern Fill with ascending digits.
#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 The operation may use all n slots. Digits 1,2,3 are written. Returning n keeps size 3. Example 3 — Return Smaller Size Allocate room for 5 but keep 1.
#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 Buffer space for 5 is prepared. Only one character is committed. Final size is 1. Frequently Asked Questions What is resize_and_overwrite? C++23 API that lets you write directly into a resized string buffer through a callback.
What must op return? The number of characters that should become the new size, <= n.
Why use it? Avoid temporary buffers when filling a string from C APIs or custom writers.
Fallback on older C++? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion