C++ std::remove_copy_if() Function

Definition
In C++, std::remove_copy_if() copies elements to a destination but skips every element for which the predicate returns true.
It builds a filtered copy and leaves the source alone. It is the opposite of std::copy_if(), because the predicate selects what to skip.
Syntax
Add #include <algorithm>, then call std::remove_copy_if(). Start with these two lines.
std::remove_copy_if(src.begin(), src.end(), std::back_inserter(out), pred);
std::remove_copy_if(src.begin(), src.end(), out.begin(), pred);
| Call | What it does |
|---|
std::remove_copy_if(src.begin(), src.end(), std::back_inserter(out), pred); | Copy only the elements that fail the predicate. |
std::remove_copy_if(src.begin(), src.end(), out.begin(), pred); | Copy into a destination of enough size. |
Library declarations
template<class InputIt, class OutputIt, class UnaryPred>
OutputIt remove_copy_if(InputIt first, InputIt last,
OutputIt d_first, UnaryPred p);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The source range. |
| d_first | Required. Where the copy starts. |
| p | Required. A predicate. Elements for which it returns true are skipped. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points just after the last element written in the destination. |
C++ Examples
Example 1 — Drop odd numbers
Keep only the even numbers in a new vector.
#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::remove_copy_if(src.begin(), src.end(), std::back_inserter(out),
[](int n) { return n % 2 != 0; });
for (int x : out) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The predicate is true for odd numbers, which are skipped.
- 2, 4 and 6 are the elements that fail the predicate, so they are copied.
src keeps its six elements.
Example 2 — Strip digits
Remove digits from text while keeping the original.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
int main() {
std::string text = "a1b2c3";
std::string letters;
std::remove_copy_if(text.begin(), text.end(), std::back_inserter(letters),
[](char c) { return c >= '0' && c <= '9'; });
std::cout << letters << '\n';
return 0;
}
How It Works
- The lambda is true for digit characters.
- Those digits are skipped during the copy.
- Only the letters a, b and c reach
letters.
Example 3 — Drop low scores
Skip scores below 50.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> scores = {45, 80, 30, 95};
std::vector<int> passed;
std::remove_copy_if(scores.begin(), scores.end(), std::back_inserter(passed),
[](int s) { return s < 50; });
for (int x : passed) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- Scores below 50 make the predicate true, so they are dropped.
- 80 and 95 do not match, so they are copied.
- The original
scores vector still has all four values.
Frequently Asked Questions
In C++, std::remove_copy_if() copies a range while leaving out 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.
copy_if() copies the elements that match the predicate. remove_copy_if() copies the elements that do not match.
No. The source range is only read.
Next: remove_if()
Moves elements that do not satisfy a condition to the front and returns the new end.
remove_if() tutorial →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.
23 people found this page helpful