Algorithm Replace If Function

C++ Algorithm replaceif function is used to assign newvalue to all the elements in the range [first, last) for which pred predicate returns true.

This function examines each element in a range and replaces it if it satisfies a specified predicate.

Syntax

Example

template <class ForwardIterator, class UnaryPredicate, class T>
void replace_if (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred, 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.

pred : The unary predicate function that must be satisfied is the value of the element is to be replaced.

new_value : The new value assigned to the elements whose old value satisfies the predicate.

Return value

Complexity

Complexity is linear in the distance within first and last. Applies pred to each element and assigns to those matching also.

Data races

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

Exception safety

This function throws an exception if any of the pred, 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_if:

Example

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

using namespace std;

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

  replace_if(v.begin(), v.end(),
    [](int x) { return x%2 != 0; }, 10);

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

Output:

Output

10,10,2,10,2,

The above example determines odd numbers from vector v and replaces all found element with 10.

Example 2

Let's see another simple example:

Example

#include <vector>  
#include <algorithm>  
#include <iostream>  
  
bool greater6 ( int value ) {  
   return value >6;  
}  
  
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 satisfying the predicate greater6  
   // with a value of 70  
   replace_if ( v1.begin( ), v1.end( ), greater6 , 70);  
  
   cout << "The vector v1 with a value 70 replacing those\n "  
        << "elements satisfying the greater6 predicate 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 70 replacing those
 elements satisfying the greater6 predicate is:
 ( 4 70 70 70 0 5 70 1 6 70 3 70 70 2 ).

In the above example, vector v1 with a value 70 replacing those elements satisfying the greater 6 predicate.

Example 3

Let's see another simple example:

Example

#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
bool isDivisibleByThree
(
  int n //in
)
{
  return (n%3 == 0);
}
 
int main()
{
  int a[] = {1, 2, 2, 3, 4, 5, 2, 6};
  vector<int> v(a, a+8);
 
  cout <<"\nHere are the values in the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  cout <<"\nNow we replace all values divisble by 3 with 123.";
  replace_if(v.begin(), v.end(), isDivisibleByThree, 123);
 
  cout <<"\nHere are the revised contents of the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  return 0;
}

Output:

Output

Here are the values in the vector:
1 2 2 3 4 5 2 6 
Now we replace all values divisible by 3 with 123.
Here are the revised contents of the vector:
1 2 2 123 4 5 2 123

Example 4

Let's see another simple example:

Example

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

using namespace std;

bool IsOdd (int i) { return ((i%2)==1); }

int main () {
  vector<int> myvector;

  // set some values:
  for (int i=1; i<10; i++) myvector.push_back(i);               // 1 2 3 4 5 6 7 8 9

  replace_if (myvector.begin(), myvector.end(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0

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

  return 0;
}

Output:

Output

myvector contains: 0 2 0 4 0 6 0 8 0

Input Required

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