C++ std::replace() Function

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.
std::replace(v.begin(), v.end(), 2, 9);
std::replace(s.begin(), s.end(), ' ', '_');
| Call | What 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
template<class ForwardIt, class T>
void replace(ForwardIt first, ForwardIt last,
const T& old_value, const T& new_value);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to change. |
| old_value | Required. The value to look for. |
| new_value | Required. The value to write instead. |
Return Value
| Return Value | Description |
|---|
void | Nothing is returned. |
C++ Examples
Example 1 — Replace a number
Change every 2 into 9.
#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
replace() compares each element with the old value 2.- Three elements match, at index 1, 3 and 4.
- Those three are overwritten with 9. The others stay.
Example 2 — Replace spaces
Turn a sentence into an underscore name.
#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
- Both values are characters, so they use single quotes.
- Each space is changed into an underscore.
- The string length stays the same.
Example 3 — Replace in part of a range
Limit the change to the first two elements.
#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
v.begin() + 2 ends the range after two elements.- Only those two elements are checked and changed.
- 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.
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.
16 people found this page helpful