std::replace, std::replace_if

From cppreference.com
< cpplrm; | algorithm
Algorithm library
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
replacereplace_if
(until C++17)

Operations on uninitialized storage
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)
Permutations
Numeric operations
C library
Defined in header <algorithm>
(1)
template< class ForwardIt, class T >

void replace( ForwardIt first, ForwardIt last,

const T& old_value, const T& new_value );
(until C++20)
template< class ForwardIt, class T >

constexpr void replace( ForwardIt first, ForwardIt last,

const T& old_value, const T& new_value );
(since C++20)
template< class ExecutionPolicy, class ForwardIt, class T >

void replace( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,

const T& old_value, const T& new_value );
(2) (since C++17)
(3)
template< class ForwardIt, class UnaryPredicate, class T >

void replace_if( ForwardIt first, ForwardIt last,

UnaryPredicate p, const T& new_value );
(until C++20)
template< class ForwardIt, class UnaryPredicate, class T >

constexpr void replace_if( ForwardIt first, ForwardIt last,

UnaryPredicate p, const T& new_value );
(since C++20)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate, class T >

void replace_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,

UnaryPredicate p, const T& new_value );
(4) (since C++17)

Replaces all elements satisfying specific criteria with new_value in the range [first, last).

1) Replaces all elements that are equal to old_value.
3) Replaces all elements for which predicate p returns true.
2,4) Same as (1,3), but executed according to policy. These overloads do not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true

Parameters

first, last - the range of elements to process
old_value - the value of elements to replace
policy - the execution policy to use. See execution policy for details.
p - unary predicate which returns true if the element value should be replaced.

The signature of the predicate function should be equivalent to the following:

bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to Type.

new_value - the value to use as replacement
Type requirements
-
ForwardIt must meet the requirements of ForwardIterator.
-
UnaryPredicate must meet the requirements of Predicate.

Return value

(none)

Complexity

Exactly last - first applications of the predicate.

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the three standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Notes

Because the algorithm takes old_value and new_value by reference, it can have unexpected behavior if either is a reference to an element of the range [first, last).

Possible implementation

First version
template<class ForwardIt, class T>
void replace(ForwardIt first, ForwardIt last,
             const T& old_value, const T& new_value)
{
    for (; first != last; ++first) {
        if (*first == old_value) {
            *first = new_value;
        }
    }
}
Second version
template<class ForwardIt, class UnaryPredicate, class T>
void replace_if(ForwardIt first, ForwardIt last,
                UnaryPredicate p, const T& new_value)
{
    for (; first != last; ++first) {
        if(p(*first)) {
            *first = new_value;
        }
    }
}

Example

The following code at first replaces all occurrences of 8 with 88 in a vector of integers. Then it replaces all values less than 5 with 55.

#include <algorithm>
#include <array>
#include <iostream>
#include <functional>

int main()
{
    std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};

    std::replace(s.begin(), s.end(), 8, 88);

    for (int a : s) {
        std::cout << a << " ";
    }
    std::cout << '\n';

    std::replace_if(s.begin(), s.end(), 
                    std::bind(std::less<int>(), std::placeholders::_1, 5), 55);
    for (int a : s) {
        std::cout << a << " ";
    }
    std::cout << '\n';
}

Output:

5 7 4 2 88 6 1 9 0 3
5 7 55 55 88 6 55 9 55 55

See also

copies a range, replacing elements satisfying specific criteria with another value
(function template)