std::replace_copy_if() algorithm
- od C++20
- od C++17
- do C++17
// (1)
template< class InputIt, class OutputIt, class UnaryPredicate, class T >
constexpr OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first,
UnaryPredicate p, const T& new_value );
// (2)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class UnaryPredicate, class T >
ForwardIt2 replace_copy_if( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first,
UnaryPredicate p, const T& new_value );
// (1)
template< class InputIt, class OutputIt, class UnaryPredicate, class T >
OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first,
UnaryPredicate p, const T& new_value );
// (2)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class UnaryPredicate, class T >
ForwardIt2 replace_copy_if( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first,
UnaryPredicate p, const T& new_value );
// (1)
template< class InputIt, class OutputIt, class UnaryPredicate, class T >
OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first,
UnaryPredicate p, const T& new_value );
-
(1) Copies the elements from the range [
first
;last
) to another range beginning atd_first
, while replacing elements that satisfy prediatep
withnew_value
(compared usingoperator==
). -
(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::remove_cvref_t<ExecutionPolicy>>
(od C++20) istrue
.
Parameters
first last | The range of elements to copy. |
d_first | The beginning of the destination range. |
policy | The execution policy to use. See execution policy for details. |
p | Unary predicate which returns The expression |
Type requirements
InputIt | LegacyInputIterator |
OutputIt | LegacyOutputIterator |
ForwardIt1 ForwardIt2 | LegacyForwardIterator |
Predicate | UnaryPredicate |
The results of the expressions *first
and new_value
must be writable to d_first
.
Return value
Iterator to the element past the last element copied.
Complexity
Given N
as std::distance(first, last)
:
Exactly N applications of the 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_copy_if (1)
template<class InputIt, class OutputIt, class UnaryPredicate, class T>
OutputIt replace_copy_if(InputIt first, InputIt last, OutputIt d_first,
UnaryPredicate p, const T& new_value)
{
for (; first != last; ++first)
*d_first++ = p(*first) ? new_value : *first;
return d_first;
}
Examples
The following copy-prints a vector, replacing all values over 5
with 99
on the fly.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> v {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
std::replace_copy_if(v.begin(), v.end(),
std::ostream_iterator<int>(std::cout, " "),
[](int n){ return n > 5; }, 99);
std::cout << '\n';
}
5 99 4 2 99 99 1 99 0 3
Hover to see the original license.