Assigns new_value to all the elements in the range [first, last] that compare to old_value. The function use operator == to compare the individual elements to old_value
Function Template :
void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value) first, last : Forward iterators to the initial and final positions in a sequence of elelments. old_value : Value to be replaced. new_value : Replacement value.
Return Value :
This function do not return any value. If elements that needs to be replace is found then element
replaced otherwise remain unchanged.
Input : 10 20 30 30 20 10 10 20 Output : 10 99 30 30 99 10 10 99 // Replaced value 20 in vector to 99. Input : 3 5 7 8 9 5 4 Output : 3 5 7 12 9 5 4 // Replaced value 8 by 12.
Original Array: 10 20 30 30 20 10 10 20 New Array: 10 99 30 30 99 10 10 99
Assigns new_value to all the elements in range [first, last] for which pred returns true.
Function Template :
void replace_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred, const T& new_value) first, last : Forward iterators to the initial and final positions in a sequence of elelments. pred : Unary function that accepts an element in the range as argument, and returns a value convertible to bool.The returned value indicate whether the element is to be replaced (if true, it is replaced). The function shall not modify its argument. old_value : Value to be replaced. new_value : Replacement value.
Input : 1 2 3 4 5 6 7 8 9 10 Output : 0 2 0 4 0 6 0 8 0 10 // Replaced all odd values to 0. Input : 10 20 30 30 20 10 10 20 Output : 10 4 30 30 4 10 10 4 // Replaced all number divisible by 4 to 4.
Original Array: 1 2 3 4 5 6 7 8 9 10 New Array: 0 2 0 4 0 6 0 8 0 10
Also you can add any kind of function in std::replace_if that can only have one argument only.
This article is contributed by Sachin Bisht. If you like GeeksforGeeks 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.
- isupper() and islower() and their application in C++
- Get first and last elements from Array and Vector in CPP
- Design and Implement Special Stack Data Structure | Added Space Optimized Version
- unordered_multiset and its uses
- Generating Test Cases (generate() and generate_n() in C++)
- Sum of all elements between k1'th and k2'th smallest elements
- C++ bitset and its application
- C++ string class and its applications
- unordered_multimap and its application
- fill() and fill_n() functions in C++ STL
- Type Inference in C++ (auto and decltype)
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Design a stack that supports getMin() in O(1) time and O(1) extra space
- Sorting 2D Vector in C++ | Set 1 (By row and column)
- std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
- Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
- Forward List in C++ | Set 1 (Introduction and Important Functions)
- accumulate() and partial_sum() in C++ STL : numeric header