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.
- cin get() in C++ with Examples
- Difference between Increment and Decrement Operators
- Map of Vectors in C++ STL with Examples
- Problem in comparing Floating point numbers and how to compare them correctly?
- exit(0) vs exit(1) in C/C++ with Examples
- C++ program to print all Even and Odd numbers from 1 to N
- Reverse the content of a file and store it in another
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Left-Right traversal of all the levels of N-ary tree
- Difference between Iterators and Pointers in C/C++ with Examples
- ostream::seekp(pos) method in C++ with Exmaples
- Default Methods in C++ with Examples