Algorithm Replace Function

C++ Algorithm replace function is used to replace all value equal to oldvalue by the value newvalue in the range [first, last).

This function examines each element in the range and replaces it if it matches a specified value.

Syntax

Example

template <class ForwardIterator, class T>
  void replace (ForwardIterator first, ForwardIterator last,
                const T& old_value, const T& new_value);

Parameter

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

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

old_value : The old value of the elements to be replaced.

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

Return value

Complexity

Complexity is linear in the distance within first and last. It compares each element and assigns to those matching.

Data races

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

Exception safety

Throws an exception if any of the function calls the 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:

Example

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

using namespace std;

int main() {
  vector<int> v = { 3,1,2,1,2 };

  replace(v.begin(), v.end(), 1, 10);

  for_each(v.begin(), v.end(),
    [](int x) { cout << x << ","; });
    
    return 0;
}

Output:

Output

3,10,2,10,2,

In the above example, element 1 of vector v is replaced by 10.

Example 2

Let's see another simple example:

Example

#include <vector>  
#include <algorithm>  
#include <iostream>  
  
int main( ) {  
   using namespace std;  
   vector <int> v1;  
   vector <int>::iterator 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( ) );  
   cout << "The original vector v1 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
   // Replace elements with a value of 7 with a value of 700  
   replace (v1.begin( ), v1.end( ), 7 , 700);  
  
   cout << "The vector v1 with a value 700 replacing that of 7 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *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 ).
The vector v1 with a value 700 replacing that of 7 is:
 ( 4 700 700 700 0 5 700 1 6 9 3 700 8 2 ).

In the above example, replace finds all the elements from vector v1 matches with 7 and replace it with the 700.

Example 3

Let's see another simple example:

Example

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

using namespace std;
 
void print(vector<int>& v)
{
    for(int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
}
 
int main() {
    vector<int> v = {1, 4, 3, 2, 3, 10, 7, 9, 3, 8};
 
    cout << "v : ";
    print(v);
    // replace 3 with 6
    replace(v.begin(), v.end(), 3, 6);
    cout << "After replacing 3 with 6\n";
    cout << "v : ";
    print(v);
    
    return 0;
}

Output:

Output

v : 1 4 3 2 3 10 7 9 3 8 
After replacing 3 with 6
v : 1 4 6 2 6 10 7 9 6 8

Example 4

Let's see another simple example:

Example

#include <iostream>     // cout
#include <algorithm>    // replace
#include <vector>       // vector

using namespace std;

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

  replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 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 30 99 10 10 99

Input Required

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