Home C++ Strings Functions replace_with_range() C++ std::string replace_with_range() Function Definition In C++, std::string::replace_with_range() replaces an iterator range with characters from another range (C++23).
Include <string> and use C++23.
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 [first, last) and the replacement range.
s.replace_with_range(s.begin() + 1, s.begin() + 3, vec); Call What it does s.replace_with_range(first, last, rg)Replace [first, last) with characters from rg.
Older alternative: s.replace(first, last, rg.begin(), rg.end()) with iterator-pair overloads.
Library declarations template<class R>
constexpr iterator replace_with_range(const_iterator first, const_iterator last, R&& rg); // C++23 Parameters Parameter Description first, lastHalf-open range inside the string to remove. rgReplacement character range.
Return Value Return Value Description iterator Iterator to the first character of the inserted content (or last if rg empty).
C++ Examples 1. Replace middle 2. With view 3. Empty replacement Example 1 — Replace Middle Range Swap two chars for a vector of chars.
#include <iostream>
#include <string>
#include <vector>
int main() {
std::string s = "aXYb";
std::vector<char> v = {'1', '2', '3'};
s.replace_with_range(s.begin() + 1, s.begin() + 3, v);
std::cout << s << '\\n';
return 0;
} How It Works [begin+1, begin+3) selects "XY". Those two chars are removed. Vector chars 1,2,3 are inserted. Example 2 — Replace Using string_view Replace a slice with a view.
#include <iostream>
#include <string>
#include <string_view>
int main() {
std::string s = "I use Java";
s.replace_with_range(s.begin() + 6, s.end(), std::string_view{"C++"});
std::cout << s << '\\n';
return 0;
} How It Works From index 6 to end is "Java". The view supplies "C++". Final sentence uses C++. Example 3 — Replace With Empty Range Deleting a slice by replacing with nothing.
#include <iostream>
#include <string>
#include <vector>
int main() {
std::string s = "abXYcd";
std::vector<char> none;
s.replace_with_range(s.begin() + 2, s.begin() + 4, none);
std::cout << s << '\\n';
return 0;
} How It Works The selected slice is "XY". Empty replacement removes it. Similar effect to erase on that range. Frequently Asked Questions What is replace_with_range? C++23 API to replace an iterator range with another range of characters.
Can rg be empty? Yes—then the effect is like erase on that span.
Fallback? Use classic replace with iterator pairs.
Does it return an iterator? Yes, to the start of the newly inserted characters.
Next: reserve() Keep going through the string functions. Request that the string buffer hold at least n characters without reallocating.
reserve() 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.
9 people found this page helpful
Helpful Share Copy link Suggestion