C++ std::remove_copy() Function

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

Definition

In C++, std::remove_copy() copies the elements of a range to a destination, skipping every element equal to a given value.

The source is not changed. It is a clean way to build a filtered copy without the erase-remove step.

Syntax

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

C++
std::remove_copy(src.begin(), src.end(), std::back_inserter(out), 2);
std::remove_copy(src, src + 5, dst, 0);
CallWhat it does
std::remove_copy(src.begin(), src.end(), std::back_inserter(out), 2);Copy everything except the value 2.
std::remove_copy(src, src + 5, dst, 0);Works with plain arrays and pointers too.
Library declarations
C++
template<class InputIt, class OutputIt, class T>
OutputIt remove_copy(InputIt first, InputIt last,
                     OutputIt d_first, const T& value);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_firstRequired. Where the copy starts.
valueRequired. Elements equal to this value are skipped.

Return Value

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

C++ Examples

Example 1 — Skip one value

Copy a vector without any 2s.

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

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

    std::remove_copy(src.begin(), src.end(), std::back_inserter(out), 2);

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

How It Works

  1. remove_copy() reads each value and writes it only if it is not 2.
  2. std::back_inserter makes out grow with each written value.
  3. The source still has five elements, because it was not changed.

Example 2 — Skip a letter

Build a string without the letter a.

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

int main() {
    std::string word = "banana";
    std::string out;

    std::remove_copy(word.begin(), word.end(), std::back_inserter(out), 'a');

    std::cout << out << '\n';
    return 0;
}

How It Works

  1. The source is banana, a range of six characters.
  2. Each 'a' is skipped during the copy.
  3. Only b, n and n are written to out.

Example 3 — Copy into an array

Use the returned pointer to count what was copied.

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

int main() {
    int src[] = {4, 0, 5, 0, 6};
    int dst[5] = {};

    int* end = std::remove_copy(src, src + 5, dst, 0);

    std::cout << "Copied " << (end - dst) << ": ";
    for (int* p = dst; p != end; ++p) {
        std::cout << *p << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Plain arrays and pointers work as iterators.
  2. The two zeros are skipped, so only three values are copied.
  3. The returned pointer end marks the end of the copied data inside dst.

Frequently Asked Questions

In C++, std::remove_copy() copies a range while leaving out elements equal to a value. 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.
remove() changes the original range. remove_copy() leaves the source alone and writes the kept elements elsewhere.
No. The source is never changed, so there is nothing to erase.

Next: remove_copy_if()

Copies a range while leaving out elements that satisfy a condition.

remove_copy_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.

14 people found this page helpful