# 11 Algorithms library [algorithms]

## 11.4 Mutating sequence operations [alg.modifying.operations]

### 11.4.5 Replace [alg.replace]

``` template <InputIterator I, Sentinel<I> S, class T1, class T2, class Proj = identity> requires Writable<I, const T2&> && IndirectRelation<equal_to<>, projected<I, Proj>, const T1*> I replace(I first, S last, const T1& old_value, const T2& new_value, Proj proj = Proj{}); template <InputRange Rng, class T1, class T2, class Proj = identity> requires Writable<iterator_t<Rng>, const T2&> && IndirectRelation<equal_to<>, projected<iterator_t<Rng>, Proj>, const T1*> safe_iterator_t<Rng> replace(Rng&& rng, const T1& old_value, const T2& new_value, Proj proj = Proj{}); template <InputIterator I, Sentinel<I> S, class T, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> requires Writable<I, const T&> I replace_if(I first, S last, Pred pred, const T& new_value, Proj proj = Proj{}); template <InputRange Rng, class T, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<Rng>, Proj>> Pred> requires Writable<iterator_t<Rng>, const T&> safe_iterator_t<Rng> replace_if(Rng&& rng, Pred pred, const T& new_value, Proj proj = Proj{}); ```

Effects: Assigns new_value through each iterator i in the range [first,last) when the following corresponding conditions hold: invoke(proj, *i) == old_value, invoke(pred, invoke(proj, *i)) != false.

Returns: last.

Complexity: Exactly last - first applications of the corresponding predicate and projection.

``` template <InputIterator I, Sentinel<I> S, class T1, class T2, OutputIterator<const T2&> O, class Proj = identity> requires IndirectlyCopyable<I, O> && IndirectRelation<equal_to<>, projected<I, Proj>, const T1*> tagged_pair<tag::in(I), tag::out(O)> replace_copy(I first, S last, O result, const T1& old_value, const T2& new_value, Proj proj = Proj{}); template <InputRange Rng, class T1, class T2, OutputIterator<const T2&> O, class Proj = identity> requires IndirectlyCopyable<iterator_t<Rng>, O> && IndirectRelation<equal_to<>, projected<iterator_t<Rng>, Proj>, const T1*> tagged_pair<tag::in(safe_iterator_t<Rng>), tag::out(O)> replace_copy(Rng&& rng, O result, const T1& old_value, const T2& new_value, Proj proj = Proj{}); template <InputIterator I, Sentinel<I> S, class T, OutputIterator<const T&> O, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> requires IndirectlyCopyable<I, O> tagged_pair<tag::in(I), tag::out(O)> replace_copy_if(I first, S last, O result, Pred pred, const T& new_value, Proj proj = Proj{}); template <InputRange Rng, class T, OutputIterator<const T&> O, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<Rng>, Proj>> Pred> requires IndirectlyCopyable<iterator_t<Rng>, O> tagged_pair<tag::in(safe_iterator_t<Rng>), tag::out(O)> replace_copy_if(Rng&& rng, O result, Pred pred, const T& new_value, Proj proj = Proj{}); ```

Requires: The ranges [first,last) and [result,result + (last - first)) shall not overlap.

Effects: Assigns to every iterator i in the range [result,result + (last - first)) either new_value or *(first + (i - result)) depending on whether the following corresponding conditions hold:

```invoke(proj, *(first + (i - result))) == old_value
invoke(pred, invoke(proj, *(first + (i - result)))) != false
```

Returns: {last, result + (last - first)}.

Complexity: Exactly last - first applications of the corresponding predicate and projection.