Replace Elements and Ranges

Let's look at the functions we can use to update and replace values in ranges.

You have with std::replace, std::replace_if, std::replace_copy and std::replace_copy_if four variations to replace elements in a range. The algorithms differ in two aspects. First, does the algorithm need a predicate? Second, does the algorithm copy the elements in the destination range?

replace: Replaces the old elements in the range with newValue, if the old element has the value old.

Get hands-on with 1200+ tech skills courses.