Algorithm Replace Copy Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Replace Copy Function

Algorithm Replace Copy Function

BLUF: Mastering Algorithm Replace Copy Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm Replace Copy Function

C++ is renowned for its efficiency. Learn how Algorithm Replace Copy Function enables low-level control and high-performance computing in the tutorial below.

The C++ algorithm replacecopy function is employed to duplicate the range [first, last) while substituting all occurrences of the oldvalue with the new_value. This function utilizes the operator= for copying elements and the operator== for comparing them.

This function scrutinizes every item within a given range and substitutes it with a designated value, then transfers the updated content to a different 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

An input iterator referencing the starting point within the range where elements are being substituted.

last: An input iterator indicating the ultimate position within the range where elements are being substituted.

result: An output iterator indicating the initial element of the range where the outcome sequence is stored.

The previous value: The value that was initially assigned to the element before it was replaced.

The updated data assigned to the components with the previous value.

Return value

The replace_copy function produces an output iterator that points to the location where the last item written in the resulting sequence is placed.

Complexity

The time complexity grows linearly based on the distance between the first and last elements, executing a comparison and assignment operation for every element in between.

Data races

The elements within the range [first1, last1) are retrieved.

The items within the specified range in the output and the value that is returned undergo alterations.

Exception safety

This function will raise an exception if any errors occur during element comparison, element assignments, or iterator operations.

Please be aware that providing incorrect parameters can lead to unpredictable outcomes.

Example 1

Let's explore a straightforward example to showcase the functionality of the replace_copy method:

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 provided example, the function replace_copy is employed to substitute the character 'A' at the beginning of vector v with 'Z' and then duplicate it to the initial position of v.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience