std::replace and std::replace_if in C++
Last Updated :
26 Apr, 2022
std::replace
Assigns new_value to all the elements in the range [first, last) that compare to old_value. The function use operator == to compare the individual elements to old_value
Function Template :
void replace (ForwardIterator first,
ForwardIterator last,
const T& old_value,
const T& new_value)
first, last :
Forward iterators to the initial and final positions
in a sequence of elements.
old_value : Value to be replaced.
new_value : Replacement value.
Return Value :
This function do not return any value. If elements that needs to be replace is found then element
replaced otherwise remain unchanged.
Examples:
Input : 10 20 30 30 20 10 10 20
Output : 10 99 30 30 99 10 10 99
// Replaced value 20 in vector to 99.
Input : 3 5 7 8 9 5 4
Output : 3 5 7 12 9 5 4
// Replaced value 8 by 12.
CPP
// CPP program to find and replace the value
// with another value in array
// using std::replace
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
int arr[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
int n = sizeof(arr) / sizeof(arr[0]);
// variable containing the old and new values
int old_val = 20, new_val = 99;
// print old array
cout << "Original Array:";
for (int i = 0; i < n; i++)
cout << ' ' << arr[i];
cout << '\n';
// Function used to replace the values
replace(arr, arr + n, old_val, new_val);
// new array after using std::replace
cout << "New Array:";
for (int i = 0; i < n; i++)
cout << ' ' << arr[i];
cout << '\n';
return 0;
}
OutputOriginal Array: 10 20 30 30 20 10 10 20
New Array: 10 99 30 30 99 10 10 99
std::replace_if
Assigns new_value to all the elements in range [first, last) for which pred returns true.
Function Template :
void replace_if (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred, const T& new_value)
first, last : Forward iterators to the initial and final positions
in a sequence of elelments.
pred : Unary function that accepts an element in the range as argument, and
returns a value convertible to bool.The returned value indicate whether
the element is to be replaced (if true, it is replaced).
The function shall not modify its argument.
old_value : Value to be replaced.
new_value : Replacement value.
Examples:
Input : 1 2 3 4 5 6 7 8 9 10
Output : 0 2 0 4 0 6 0 8 0 10
// Replaced all odd values to 0.
Input : 10 20 30 30 20 10 10 20
Output : 10 4 30 30 4 10 10 4
// Replaced all number divisible by 4 to 4.
CPP
// CPP code to find all the elements that are odd
// and replace them with 0.
// using std::replace_if
#include <bits/stdc++.h>
using namespace std;
// Function that is used in std::replace_if
// If number is odd return 1, else 0
// 1 (True) means replace the number
// 0 (False) means does not replace
bool IsOdd(int i)
{
return ((i % 2) == 1);
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
// print old array
cout << "Original Array:";
for (int i = 0; i < n; i++)
cout << ' ' << arr[i];
cout << '\n';
// replacement value
int new_val = 0;
// replace_if function
replace_if(arr, arr + n, IsOdd, new_val);
// new array after using std::replace
cout << "New Array:";
for (int i = 0; i < n; i++)
cout << ' ' << arr[i];
cout << '\n';
return 0;
}
OutputOriginal Array: 1 2 3 4 5 6 7 8 9 10
New Array: 0 2 0 4 0 6 0 8 0 10
Also you can add any kind of function in std::replace_if that can only have one argument only.
Similar Reads
std::string::replace_copy(), std::string::replace_copy_if in C++ replace_copy replace_copy() is a combination of copy() and replace(). It copies the elements in the range [first, last) to the range beginning at result, replacing the appearances of old_value by new_value.The range copied is [first, last), which contains all the elements between first and last, inc
4 min read
std::string::replace() in C++ The string::replace() function in C++ is used to replace a single or multiple characters from a given index. It is the member function of std::string class. In this article, we will learn how to use the string::replace() function in our C++ program.SyntaxThe string::replace() function provides 6 dif
6 min read
std::remove_if in C++ STL std::remove_if() is a built-in algorithm of the C++ STL that is used to remove elements from a specified range of elements that satisfies the given condition. The condition can be defined as a function, lambda expression or a function object. It is defined inside the <algorithm> header file.In
4 min read
list::emplace_front() and list::emplace_back() in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::emplace_front()This function is used to ins
3 min read
list::remove() and list::remove_if() in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::remove() remove() function is used to remov
3 min read