C++ std::string replace_with_range() Function

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

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.

C++
s.replace_with_range(s.begin() + 1, s.begin() + 3, vec);
CallWhat 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
C++
template<class R>
constexpr iterator replace_with_range(const_iterator first, const_iterator last, R&& rg); // C++23

Parameters

ParameterDescription
first, lastHalf-open range inside the string to remove.
rgReplacement character range.

Return Value

Return ValueDescription
iteratorIterator to the first character of the inserted content (or last if rg empty).

C++ Examples

Example 1 — Replace Middle Range

Swap two chars for a vector of chars.

C++
#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

  1. [begin+1, begin+3) selects "XY".
  2. Those two chars are removed.
  3. Vector chars 1,2,3 are inserted.

Example 2 — Replace Using string_view

Replace a slice with a view.

C++
#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

  1. From index 6 to end is "Java".
  2. The view supplies "C++".
  3. Final sentence uses C++.

Example 3 — Replace With Empty Range

Deleting a slice by replacing with nothing.

C++
#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

  1. The selected slice is "XY".
  2. Empty replacement removes it.
  3. Similar effect to erase on that range.

Frequently Asked Questions

C++23 API to replace an iterator range with another range of characters.
C++23.
Yes—then the effect is like erase on that span.
Use classic replace with iterator pairs.
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.

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.

9 people found this page helpful