std::replace_if() algorithm
- od C++20
- od C++17
- do C++17
// (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)
template< class ForwardIt, class UnaryPredicate, class T >
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)
template< class ForwardIt, class UnaryPredicate, class T >
void replace_if( ForwardIt first, ForwardIt last,
UnaryPredicate p, const T& new_value );
-
(1) Replaces all elements for which predicate
p
returnstrue
withnew_value
in the range [first
;last
). -
(2) Same as (1), but executed according to
policy
.Overload ResolutionThese 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) istrue
.
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 The expression |
Type requirements
ForwardIt | LegacyForwardIterator |
UnaryPredicate | Predicate |
- od C++20
- do C++20
*first | Must be writable to d_first . |
*first = new_value
must be valid.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 otherExecutionPolicy
, 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
.
#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';
}
5 7 4 2 88 6 1 9 0 3
5 7 55 55 88 6 55 9 55 55
Hover to see the original license.