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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to iterate over the elements of an std::tuple in C++
- How to access private/protected method outside a class in C++
- lvalues references and rvalues references in C++ with Examples
- Difference between cout and std::cout in C++
- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- How to use unordered_map efficiently in C++
- Header files in C/C++ with Examples
- C++ Program for the Fractional Knapsack Problem
- Trigraphs in C++ with Examples
- Difference Between DART and C++
- How to fix auto keyword error in Dev-C++
- Lexicographical smallest number after at most K consecutive swaps
- Important functions of STL Components in C++
- Array of Structures vs. Array within a Structure in C/C++