It replaces each element in the range [first, last) that is equal to oldValue with newValue. The function uses operator == to compare the individual elements to old_value.
template <class ForwardIterator, class T> void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value); first, last : the range of elements to process old_value : the value of elements to replace new_value : the value to use as replacement
Demonstration of replace Hello World ! Hello Geeks ! Hello to all ! Hello to all!!!!
replace_if() replaces each element in the range [first, last) for which the unary predicate i.e. op(elem) yields true with newValue.
template <class ForwardIterator, class UnaryPredicate, class T> void replace_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred, const T& new_value ); Unary predicate are just functions that take a single parameter and return a Boolean value. It is used to check whether an element fits the criterion
Before replace_if : A B C D E F G After replace_if : V B C D V F G
Related Article : std::string::replace_copy(), std::string::replace_copy_if
This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Maximum absolute difference in an array
- Count the pairs of vowels in the given string
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Print path from root to all nodes in a Complete Binary Tree
- Count the number of 1's and 0's in a binary array using STL in C++ ?
- Find elements of an Array which are Odd and Even using STL in C++
- Find elements of an array which are divisible by N using STL in C++
- All reverse permutations of an array using STL in C++
- Generics in C++
- Find the longest subsequence of an array having LCM at most K
- Maximum Subarray Sum after inverting at most two elements
- Maximum element in a sorted and rotated array
- What does main() return in C and C++?
- SDL library in C/C++ with examples