Home C++ Algorithms Functions replace_copy_if() C++ std::replace_copy_if() Function 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.
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); Call What 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 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 Parameter Description first, last Required. The source range. d_first Required. Where the copy starts. p Required. A predicate that returns true for elements to replace. new_value Required. The value written instead.
Return Value Return Value Description Iterator Points just after the last element written in the destination.
C++ Examples 1. Zero out even numbers 2. Cap high scores 3. Fill empty names Example 1 — Zero out even numbers Copy a vector but turn the even numbers into 0.
#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 The predicate is true for even numbers. 2, 4 and 6 are written as 0 in out. 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.
#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 The test s > 100 finds scores that are too high. 120 and 150 are written as 100. 85 and 99 are valid, so they pass through. Example 3 — Fill empty names Replace blank strings with the word unknown.
#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 s.empty() is true for the two blank names.In the new vector those are replaced by unknown. The original names vector still contains the blank strings. Frequently Asked Questions What does std::replace_copy_if() do? In C++, std::replace_copy_if() copies a range while replacing the elements that satisfy a condition. Include <algorithm> to use it.
Which header and C++ version do I need for std::replace_copy_if()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::replace_copy_if() return? It returns an iterator to the position after the last element written in the destination.
How is replace_copy_if() different from replace_if()? replace_if() changes the original range. replace_copy_if() writes the changed values to a new place and leaves the source as it was.
Does it remove elements? No. The output has the same number of elements as the input. Some of them just have new values.
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.
11 people found this page helpful
Helpful Share Copy link Suggestion