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.
- Rearrange characters in a string such that no two adjacent are same using hashing
- Remove duplicate elements in an Array using STL in C++
- r-Nearest neighbors
- Increment (++) and Decrement (--) operator overloading in C++
- std::uniform_int_distribution max() method in C++ with examples
- boost::algorithm::clamp() in C++ library
- std::uniform_real_distribution max() method in C++ with Examples
- std::uniform_real_distribution reset() method in C++ with Examples
- std::uniform_real_distribution a() method in C++ with Examples
- std::uniform_int_distribution min() method in C++ with examples
- std::uniform_real_distribution b() method in C++ with Examples
- std::uniform_int_distribution b() method in C++ with examples
- std::uniform_int_distribution reset() method in C++ with examples
- std::uniform_real_ distribution class in C++ with Examples
- boost::algorithm::equal() in C++ library