I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com

remove_copy

Copies elements that do not match a given value
+ View version details

Definition

The remove_copy() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template < class InputIterator, class OutputIterator, class Type >
   OutputIterator remove_copy(
      InputIterator first, 
      InputIterator last, 
      OutputIterator result,
      const Type& val
   );

Parameters:
Parameter Description
first An input iterator addressing the position of the first element in the range from which elements are being removed
last An input iterator addressing the position one past the final element in the range from which elements are being removed
result An output iterator addressing the position of the first element in the destination range to which elements are being removed
val The value that is to be removed from the range

Description

Remove_copy function copies all elements not equal to val from the range [first, last) to result.

Return Value

Returns a forward iterator addressing the new end position of the destination range, one past the final element of the copy of the remnant sequence free of the specified value.

Complexity

The complexity is linear; there are last - first comparisons for equality and last - first assignments.
Example:

Example - remove_copy algorithm
Problem
This program illustrates the use of the STL remove_copy() algorithm to copy all values except a particular value from one vector of integers to another.
Workings
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
  int a1[] = {1, 2, 2, 3, 4, 5, 2, 3, 6, 7, 2, 8, 3, 9, 10, 2};
  vector<int> v1(a1, a1+16);
 
  cout <<"\nHere are the sixteen values in v1:\n";
  for (vector<int>::size_type i=0; i<v1.size(); i++)
    cout <<v1.at(i)<<" ";
 
  int a2[] = {101, 102, 103, 104, 105, 106, 107, 108,109,
              110, 111, 112, 113, 114, 115, 116};
  vector<int> v2(a2, a2+16);
 
  cout <<"\nHere are the sixteen values in v2:\n";
  for (vector<int>::size_type i=0; i<v2.size(); i++)
    cout <<v2.at(i)<<" ";
 
  cout <<"\nNow we copy all values except 2 from v1 to v2,"   
         " starting at the 3rd value of v2.";
 
  vector<int>::iterator p = remove_copy(v1.begin(), v1.end(),
                                                    v2.begin()+2, 2);

cout <<"\nHere are the revised values in v2:\n"; for (vector<int>::size_type i=0; i<v2.size(); i++) cout <<v2.at(i)<<" ";   cout <<"\nThe iterator returned by the algorithm is pointing at the value" <<*p<<".";   return 0; }
Solution
Output:

Here are the sixteen values in v1:
1 2 2 3 4 5 2 3 6 7 2 8 3 9 10 2

Here are the sixteen values in v2:
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

Now we copy all values except 2 from v1 to v2, starting at the 3rd value of v2.

Here are the revised values in v2:
101 102 1 3 4 5 3 6 7 8 3 9 10 114 115 116

The iterator returned by the algorithm is pointing at the value114.
References