C++ std::replace_if() Function

Definition
In C++, std::replace_if() changes each element for which a predicate is true into a new value.
Use it when you want to replace a group of values, such as all negative numbers or all digits in a string.
Syntax
Add #include <algorithm>, then call std::replace_if(). Start with these two lines.
std::replace_if(v.begin(), v.end(), pred, 0);
std::replace_if(s.begin(), s.end(), isDigit, '#');
| Call | What it does |
|---|
std::replace_if(v.begin(), v.end(), pred, 0); | Change every element that passes pred into 0. |
std::replace_if(s.begin(), s.end(), isDigit, '#'); | Hide all digits in a string. |
Library declarations
template<class ForwardIt, class UnaryPred, class T>
void replace_if(ForwardIt first, ForwardIt last,
UnaryPred p, const T& new_value);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to change. |
| p | Required. A predicate. Elements for which it returns true are replaced. |
| new_value | Required. The value to write. |
Return Value
| Return Value | Description |
|---|
void | Nothing is returned. |
C++ Examples
Example 1 — Negative to zero
Turn all negative numbers into zero.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, -1, 4, -5, 9};
std::replace_if(v.begin(), v.end(), [](int n) { return n < 0; }, 0);
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The lambda is true for numbers below zero.
- -1 and -5 pass the test.
- Both are overwritten with 0. Positive numbers stay.
Example 2 — Mark odd numbers
Replace all odd numbers with -1.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::replace_if(v.begin(), v.end(), [](int n) { return n % 2 != 0; }, -1);
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
n % 2 != 0 is true for odd numbers.- 1, 3 and 5 pass, and each one becomes -1.
- Even numbers are left untouched.
Example 3 — Hide digits
Replace every digit in a string with a hash sign.
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string text = "a1b22c3";
std::replace_if(text.begin(), text.end(),
[](char c) { return c >= '0' && c <= '9'; }, '#');
std::cout << text << '\n';
return 0;
}
How It Works
- The lambda checks if a character is between
'0' and '9'. - Four characters are digits: 1, 2, 2 and 3.
- Each one becomes
#, and the letters stay.
Frequently Asked Questions
In C++, std::replace_if() replaces every element that satisfies a condition. 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 for which the predicate is true is changed to the new value.
Use std::replace_copy_if() to write the result into another container.
The last one. replace_if(first, last, predicate, new_value).
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.
7 people found this page helpful