C++ std::replace_if() Function

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

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.

C++
std::replace_if(v.begin(), v.end(), pred, 0);
std::replace_if(s.begin(), s.end(), isDigit, '#');
CallWhat 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
C++
template<class ForwardIt, class UnaryPred, class T>
void replace_if(ForwardIt first, ForwardIt last,
                UnaryPred p, const T& new_value);

Parameters

ParameterDescription
first, lastRequired. The range to change.
pRequired. A predicate. Elements for which it returns true are replaced.
new_valueRequired. The value to write.

Return Value

Return ValueDescription
voidNothing is returned.

C++ Examples

Example 1 — Negative to zero

Turn all negative numbers into zero.

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

  1. The lambda is true for numbers below zero.
  2. -1 and -5 pass the test.
  3. Both are overwritten with 0. Positive numbers stay.

Example 2 — Mark odd numbers

Replace all odd numbers with -1.

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

  1. n % 2 != 0 is true for odd numbers.
  2. 1, 3 and 5 pass, and each one becomes -1.
  3. Even numbers are left untouched.

Example 3 — Hide digits

Replace every digit in a string with a hash sign.

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

  1. The lambda checks if a character is between '0' and '9'.
  2. Four characters are digits: 1, 2, 2 and 3.
  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).

Next: reverse()

Reverses the order of the elements in a range.

reverse() 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.

7 people found this page helpful