Algorithm Replace Copy Function

C++ Algorithm replacecopy function is used to make a copy of the range [first, last) and replaces all oldvalue by the value new_value into it. It uses operator= to make the copy and to compare the elements it uses operator==.

This function examines each element in a source range and replaces it if it matches a specified value while copying the result into a new destination range.

Syntax

Example

template <class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy (InputIterator first, InputIterator last,
OutputIterator result, const T& old_value, const T& new_value);

Parameter

first : An input iterator pointing to the initial position in the range from which elements are being replaced.

last : A input iterator pointing to the final position in the range from which elements are being replaced.

result : An output iterator pointing to the first element of the range where the resulting sequence is stored.

old_value : The old value of the element being replaced.

new_value : The new value assigned to the elements with the old value.

Return value

replace_copy function returns an output iterator pointing to the position thacpp tutorials to the last element written in the result sequence.

Complexity

Complexity is linear in the distance within first and last and performs a comparison and an assignment for each element.

Data races

The objects in the range [first1, last1) are accessed.

The objects in the range within result and the returned value are modified.

Exception safety

This function throws an exception if any of the element comparison, the element assignments or the operations on iterators throws an exception.

Please note that invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to demonstrate the use of replace_copy:

Example

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

using namespace std;

int main() {
  vector<int> v = { 3,1,2,1,2 };
  
  replace_copy(v.begin(), v.end(),
    ostream_iterator<int>(cout, ","), 1, 10);
}

Output:

Output

3,10,2,10,2,

Example 2

Let's see another simple example:

Example

#include <iostream>     // std::cout
#include <algorithm>    // std::replace_copy
#include <vector>       // std::vector

using namespace std;

int main () {
  int myints[] = { 10, 20, 30, 15, 20, 10, 10, 20 };

  vector<int> myvector (8);
  replace_copy (myints, myints+8, myvector.begin(), 20, 99);

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

  return 0;
}

Output:

Output

myvector contains: 10 99 30 15 99 10 10 99

Example 3

Let's see another simple example:

Example

#include <vector>  
#include <list>  
#include <algorithm>  
#include <iostream>  
  
int main( ) {  
   using namespace std;  
   vector <int> v1;  
   list <int> L1 (15);  
   vector <int>::iterator Iter1;  
   list <int>::iterator L_Iter1;  
  
   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( ) );  
  
   int iii;  
   for ( iii = 0 ; iii <= 15 ; iii++ )  
      v1.push_back( 1 );  
  
   cout << "The original vector v1 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
   // Replace elements in one part of a vector with a value of 7  
   // with a value of 70 and copy into another part of the vector  
   replace_copy ( v1.begin( ), v1.begin( ) + 14,v1.end( ) -15, 7 , 70);  
  
   cout << "The vector v1 with a value 70 replacing that of 7 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
   // Replace elements in a vector with a value of 70  
   // with a value of 1 and copy into a list  
   replace_copy ( v1.begin( ), v1.begin( ) + 14,L1.begin( ), 7 , 1);  
  
   cout << "The list copy L1 of v1 with the value 0 replacing "  
        << "that of 7 is:\n ( " ;  
   for ( L_Iter1 = L1.begin( ) ; L_Iter1 != L1.end( ) ; L_Iter1++ )  
      cout << *L_Iter1 << " ";  
   cout << ")." << endl;  
   
   return 0;
}

Output:

Output

The original vector v1 is:
 ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ).
The vector v1 with a value 70 replacing that of 7 is:
 ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 1 4 70 70 70 0 5 70 1 6 9 3 70 8 2 1 ).
The list copy L1 of v1 with the value 0 replacing that of 7 is:
 ( 4 1 1 1 0 5 1 1 6 9 3 1 8 2 0 ).

Example 4

Let's see another simple example:

Example

#include <iostream> 
#include <vector> 
#include <algorithm> 
using namespace std; 
  
// Function to replace 'A' at v.begin() with Z and copy it 
// to v.begin() position 
void replace_copyDemo(vector<char>& v) 
{ 
    replace_copy(v.begin(), v.begin()+1, v.begin(), 'A', 'Z' ); 
} 
  
// Function to print content of vector 
void print(vector<char>& v) 
{ 
    int len = v.size(); 
    for (int i = 0; i < len; i++) 
        cout << v[i] << " "; 
    cout << endl; 
} 
  
// Driver code 
int main() 
{ 
      vector<char> v; 
  
    for (int i = 0; i <= 6; i++) 
        v.push_back('A' + i); 
    cout << "Before replace_copy " <<": ";  
    print(v); 
    replace_copyDemo(v); 
      
    cout << "After replace_copy " << ": "; 
    print(v); 
  
    return 0; 
}

Output:

Output

Before replace_copy : A B C D E F G 
After replace_copy : Z B C D E F G

In the above example, function replace_copy is used to replace 'A' at v.begin with Z and copy it to v.begin position.

Input Required

This code uses input(). Please provide values below: