C++ std::replace_copy_if() Function

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

Definition

In C++, std::replace_copy_if() copies a range into a destination. Each element for which the predicate is true is replaced by a new value in the copy.

The source stays untouched, so you get a cleaned-up version without changing the original.

Syntax

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

C++
std::replace_copy_if(src.begin(), src.end(), out.begin(), pred, 0);
std::replace_copy_if(src.begin(), src.end(), std::back_inserter(out), pred, value);
CallWhat it does
std::replace_copy_if(src.begin(), src.end(), out.begin(), pred, 0);Copy and replace matching elements with 0.
std::replace_copy_if(src.begin(), src.end(), std::back_inserter(out), pred, value);Append the result to a vector.
Library declarations
C++
template<class InputIt, class OutputIt, class UnaryPred, class T>
OutputIt replace_copy_if(InputIt first, InputIt last, OutputIt d_first,
                         UnaryPred p, const T& new_value);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_firstRequired. Where the copy starts.
pRequired. A predicate that returns true for elements to replace.
new_valueRequired. The value written instead.

Return Value

Return ValueDescription
IteratorPoints just after the last element written in the destination.

C++ Examples

Example 1 — Zero out even numbers

Copy a vector but turn the even numbers into 0.

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

int main() {
    std::vector<int> src = {1, 2, 3, 4, 5, 6};
    std::vector<int> out;

    std::replace_copy_if(src.begin(), src.end(), std::back_inserter(out),
                         [](int n) { return n % 2 == 0; }, 0);

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

How It Works

  1. The predicate is true for even numbers.
  2. 2, 4 and 6 are written as 0 in out.
  3. 1, 3 and 5 fail the test, so they are copied as they are.

Example 2 — Cap high scores

Limit every score to a maximum of 100.

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

int main() {
    std::vector<int> scores = {85, 120, 99, 150};
    std::vector<int> capped;

    std::replace_copy_if(scores.begin(), scores.end(), std::back_inserter(capped),
                         [](int s) { return s > 100; }, 100);

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

How It Works

  1. The test s > 100 finds scores that are too high.
  2. 120 and 150 are written as 100.
  3. 85 and 99 are valid, so they pass through.

Example 3 — Fill empty names

Replace blank strings with the word unknown.

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

int main() {
    std::vector<std::string> names = {"Ann", "", "Cy", ""};
    std::vector<std::string> out;

    std::replace_copy_if(names.begin(), names.end(), std::back_inserter(out),
                         [](const std::string& s) { return s.empty(); },
                         std::string("unknown"));

    for (const std::string& x : out) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. s.empty() is true for the two blank names.
  2. In the new vector those are replaced by unknown.
  3. The original names vector still contains the blank strings.

Frequently Asked Questions

In C++, std::replace_copy_if() copies a range while replacing the elements that satisfy 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 an iterator to the position after the last element written in the destination.
replace_if() changes the original range. replace_copy_if() writes the changed values to a new place and leaves the source as it was.
No. The output has the same number of elements as the input. Some of them just have new values.

Next: replace_if()

Replaces every element that satisfies a condition.

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

11 people found this page helpful