Przejdź do głównej zawartości

std::replace_if() algorithm

// (1)
template< class ForwardIt, class UnaryPredicate, class T >
constexpr void replace_if( ForwardIt first, ForwardIt last,
UnaryPredicate p, const T& new_value );

// (2)
template< class ExecutionPolicy, class ForwardIt,
class UnaryPredicate, class T >
void replace_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
UnaryPredicate p, const T& new_value );
  • (1) Replaces all elements for which predicate p returns true with new_value in the range [first; last).

  • (2) Same as (1), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::replace_if_cvref_t<ExecutionPolicy>>  (od C++20) is true.

Parameters

first
last

The range of elements to process.

new_value

The value to use as a replace_ifment.

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 expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT& is not allowed , nor is VT unless for VT a move is equivalent to a copy. (od C++11).

Type requirements

ForwardItLegacyForwardIterator
UnaryPredicatePredicate
*firstMust be writable to d_first.

Return value

(none)

Complexity

Given N as std::distance(first, last):

Exactly N applications of predicate p.

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 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.

Possible implementation

replace_if (1)
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;
}

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).

Examples

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.

Main.cpp
#include <algorithm>
#include <array>
#include <functional>
#include <iostream>

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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::replace_if() algorithm

// (1)
template< class ForwardIt, class UnaryPredicate, class T >
constexpr void replace_if( ForwardIt first, ForwardIt last,
UnaryPredicate p, const T& new_value );

// (2)
template< class ExecutionPolicy, class ForwardIt,
class UnaryPredicate, class T >
void replace_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
UnaryPredicate p, const T& new_value );
  • (1) Replaces all elements for which predicate p returns true with new_value in the range [first; last).

  • (2) Same as (1), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::replace_if_cvref_t<ExecutionPolicy>>  (od C++20) is true.

Parameters

first
last

The range of elements to process.

new_value

The value to use as a replace_ifment.

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 expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT& is not allowed , nor is VT unless for VT a move is equivalent to a copy. (od C++11).

Type requirements

ForwardItLegacyForwardIterator
UnaryPredicatePredicate
*firstMust be writable to d_first.

Return value

(none)

Complexity

Given N as std::distance(first, last):

Exactly N applications of predicate p.

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 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.

Possible implementation

replace_if (1)
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;
}

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).

Examples

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.

Main.cpp
#include <algorithm>
#include <array>
#include <functional>
#include <iostream>

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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.