C++ std::remove_copy_if() Function

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

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.

C++
std::remove_copy_if(src.begin(), src.end(), std::back_inserter(out), pred);
std::remove_copy_if(src.begin(), src.end(), out.begin(), pred);
CallWhat 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
C++
template<class InputIt, class OutputIt, class UnaryPred>
OutputIt remove_copy_if(InputIt first, InputIt last,
                        OutputIt d_first, UnaryPred p);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_firstRequired. Where the copy starts.
pRequired. A predicate. Elements for which it returns true are skipped.

Return Value

Return ValueDescription
IteratorPoints 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.

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::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

  1. The predicate is true for odd numbers, which are skipped.
  2. 2, 4 and 6 are the elements that fail the predicate, so they are copied.
  3. src keeps its six elements.

Example 2 — Strip digits

Remove digits from text while keeping the original.

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

  1. The lambda is true for digit characters.
  2. Those digits are skipped during the copy.
  3. Only the letters a, b and c reach letters.

Example 3 — Drop low scores

Skip scores below 50.

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

  1. Scores below 50 make the predicate true, so they are dropped.
  2. 80 and 95 do not match, so they are copied.
  3. 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

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.

23 people found this page helpful