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 firstname.lastname@example.org. 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.
- How to create an unordered_map of pairs in C++?
- Full screen OpenCV / GtK application in C++ running on Raspberry PI
- C++, OpenCV and Gtk3 Windows dev environment
- How to create an unordered_map of user defined class in C++?
- Sum of Bitwise-OR of all subarrays of a given Array | Set 2
- How to delete last element from a set in C++
- attributes in C++
- Print numbers in descending order along with their frequencies
- Find sum of a number and its maximum prime factor
- How to traverse a C++ set in reverse direction
- How to Read and Print an Integer value in C++
- Difference between Definition and Declaration
- How to traverse a STL map in reverse direction?
- CSV file management using C++
- std::any Class in C++