C++ std::replace() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::replace() goes through a range and changes each element that equals an old value into a new value.

The change happens in the same container. If you want to keep the original, use std::replace_copy() instead.

Syntax

Add #include <algorithm>, then call std::replace(). Start with these two lines.

C++
std::replace(v.begin(), v.end(), 2, 9);
std::replace(s.begin(), s.end(), ' ', '_');
CallWhat it does
std::replace(v.begin(), v.end(), 2, 9);Change every 2 into 9.
std::replace(s.begin(), s.end(), ' ', '_');Change spaces in a string into underscores.
Library declarations
C++
template<class ForwardIt, class T>
void replace(ForwardIt first, ForwardIt last,
             const T& old_value, const T& new_value);

Parameters

ParameterDescription
first, lastRequired. The range to change.
old_valueRequired. The value to look for.
new_valueRequired. The value to write instead.

Return Value

Return ValueDescription
voidNothing is returned.

C++ Examples

Example 1 — Replace a number

Change every 2 into 9.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 2, 2};

    std::replace(v.begin(), v.end(), 2, 9);

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. replace() compares each element with the old value 2.
  2. Three elements match, at index 1, 3 and 4.
  3. Those three are overwritten with 9. The others stay.

Example 2 — Replace spaces

Turn a sentence into an underscore name.

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

int main() {
    std::string text = "hello big world";

    std::replace(text.begin(), text.end(), ' ', '_');

    std::cout << text << '\n';
    return 0;
}

How It Works

  1. Both values are characters, so they use single quotes.
  2. Each space is changed into an underscore.
  3. The string length stays the same.

Example 3 — Replace in part of a range

Limit the change to the first two elements.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {5, 5, 5, 5};

    std::replace(v.begin(), v.begin() + 2, 5, 0);

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. v.begin() + 2 ends the range after two elements.
  2. Only those two elements are checked and changed.
  3. The last two fives are outside the range, so they stay.

Frequently Asked Questions

In C++, std::replace() replaces every element equal to one value with another value. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). Every element equal to the old value is changed to the new value.
Use std::replace_copy(), which writes the changed values to a second container.
replace() matches one exact value. replace_if() matches any element for which a predicate is true.

Next: replace_copy()

Copies a range while replacing one value with another.

replace_copy() tutorial →

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.

16 people found this page helpful