The C++ Algorithm replace function is employed to substitute all instances of oldvalue with newvalue within the specified range [first, last).
This function evaluates every item within the range and substitutes it if it aligns with a designated value.
Syntax
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value);
Parameter
A forward iterator indicating the starting point within the range where elements are being substituted.
The final iterator, known as last, points to the last element in the range that is being updated with new elements.
The previous value: The value of the elements that are to be substituted.
new_value: The fresh value that is being allocated to the elements previously holding the old value.
Return value
Complexity
The time complexity increases linearly based on the distance between the first and last elements. This involves comparing each element and assigning values to those that match.
Data races
The elements within the range [first1, last1) are accessed and may be altered as needed.
Exception safety
Throws an exception in case any of the function calls, assignments, or operations on iterators result in an exception.
Please be aware that using incorrect parameters can lead to unpredictable behavior.
Example 1
Let's examine a straightforward example to showcase the functionality of the replace method:
#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:
3,10,2,10,2,
In the example given, the first element of vector v is substituted with the value 10.
Example 2
Let's see another simple 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:
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 provided example, the replace function identifies all elements in the vector v1 that match the value 7 and substitutes them with 700.
Example 3
Let's see another simple 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:
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:
#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:
myvector contains: 10 99 30 30 99 10 10 99