The C++ function remove_copy is utilized to duplicate all elements that do not match the specified value from the range [first, last), ensuring that the order of the other elements remains unchanged.
This function cannot alter the size of the container.
- It returns an iterator to the new end of the range.
- Remove is stable. It means, the relative order of the elements that are not removed is remain unchanged.
- This function uses operator== to compare the elements to given val.
Syntax
template <class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T& val);
Parameter
A forward iterator indicates the location of the initial element within the range from which elements are being eliminated.
The final element in the range from which elements are being deleted is indicated by a forward iterator pointing one position beyond it.
An output iterator pointing to the starting position of the range from which elements are being deleted is returned as the result.
val : The value that needs to be eliminated from the interval [first, last).
Return value
A forward iterator pointing to the updated end position (last) of the duplicated range, encompassing all elements within [first, last) except those that are equivalent to val.
Complexity
Complexity scales linearly within the specified range [first, last): it compares each element and executes assignment for the ones that are not deleted.
Data races
The elements within the range [first, last) are retrieved.
The items within the range from the result up to the value that is returned undergo modifications.
Exception safety
This function will raise an exception if any errors occur during element comparison, element assignments, or iterator operations.
Note: Invalid parameters may cause an undefined behavior.
Example 1
Let's explore a straightforward example to showcase the functionality of the remove_copy method:
#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main() {
vector<int> v = { 2,3,1,2,1 };
remove_copy(v.begin(), v.end(),
ostream_iterator<int>(cout, ","), 1);
return 0;
}
Output:
2,3,2,
Example 2
Let's see another simple example:
#include <iostream> // std::cout
#include <algorithm> // std::remove_copy
#include <vector> // std::vector
using namespace std;
int main () {
int myints[] = {10,20,50,30,20,10,40,20};
vector<int> myvector (8);
remove_copy (myints,myints+8,myvector.begin(),20);
cout << "myvector contains:";
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
Output:
myvector contains: 10 50 30 10 40 0 0 0
Example 3
Let's explore another straightforward illustration of eliminating all the spaces from a provided text:
#include <algorithm>
#include <iterator>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "Text with some spaces";
cout << "before: " << str << "\n";
cout << "after: ";
remove_copy(str.begin(), str.end(),
ostream_iterator<char>(cout), ' ');
cout << '\n';
return 0;
}
Output:
before: Text with some spaces
after: Textwithsomespaces
Example 4
Let's see another simple example:
#include <vector>
#include <algorithm>
#include <iostream>
int main() {
using namespace std;
vector <int> v1, v2(10);
vector <int>::iterator Iter1, Iter2, new_end;
int i;
for ( i = 0 ; i <= 9 ; i++ )
v1.push_back( i );
int ii;
for ( ii = 0 ; ii <= 3 ; ii++ )
v1.push_back( 7 );
random_shuffle (v1.begin( ), v1.end( ) );
cout << "The original vector v1 is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Remove elements with a value of 7
new_end = remove_copy ( v1.begin( ), v1.end( ), v2.begin( ), 7 );
cout << "Vector v1 is left unchanged as ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
cout << "Vector v2 is a copy of v1 with the value 7 removed:\n ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")." << endl;
}
Output:
The original vector v1 is: ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ).
Vector v1 is left unchanged as ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ).
Vector v2 is a copy of v1 with the value 7 removed:
( 4 0 5 1 6 9 3 8 2 0 ).