Replace Elements and Ranges

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

There are four modifying algorithms that can be used to replace elements in a range: std::replace, std::replace_if, std::replace_copy, and std::replace_copy_if that can be used 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.