function template
<algorithm>

std::remove_copy

template <class InputIterator, class OutputIterator, class T>
  OutputIterator remove_copy (InputIterator first, InputIterator last,
                              OutputIterator result, const T& val);
Copy range removing value
Copies the elements in the range [first,last) to the range beginning at result, except those elements that compare equal to val.

The resulting range is shorter than [first,last) by as many elements as matches in the sequence, which are "removed".

The function uses operator== to compare the individual elements to val.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
template <class InputIterator, class OutputIterator, class T>
  OutputIterator remove_copy (InputIterator first, InputIterator last,
                              OutputIterator result, const T& val)
{
  while (first!=last) {
    if (!(*first == val)) {
      *result = *first;
      ++result;
    }
    ++first;
  }
  return result;
}


Parameters

first, last
Forward iterators to the initial and final positions in a sequence of elements supporting being compared to a value of type T. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
result
Output iterator to the initial position of the range where the resulting sequence is stored.
The pointed type shall support being assigned the value of an element in the range [first,last).
val
Value to be removed.

The ranges shall not overlap.

Return value

An iterator pointing to the end of the copied range, which includes all the elements in [first,last) except those that compare equal to val.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// remove_copy example
#include <iostream>     // std::cout
#include <algorithm>    // std::remove_copy
#include <vector>       // std::vector

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};               // 10 20 30 30 20 10 10 20
  std::vector<int> myvector (8);

  std::remove_copy (myints,myints+8,myvector.begin(),20); // 10 30 30 10 10 0 0 0

  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}


Output:
myvector contains: 10 30 30 10 10 0 0 0

Complexity

Linear in the distance between first and last: Compares each element, and performs an assignment operation for those not removed.

Data races

The objects in the range [first,last) are accessed.
The objects in the range between result and the returned value are modified.

Exceptions

Throws if any of the element comparisons, the element assignments or the operations on iterators throws.
Note that invalid arguments cause undefined behavior.

See also