std::replace() algorithm
- od C++20
- od C++17
- do C++17
// (1)
template< class ForwardIt, class T >
constexpr void replace( ForwardIt first, ForwardIt last,
const T& old_value, const T& new_value );
// (2)
template< class ExecutionPolicy, class ForwardIt, class T >
void replace( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
const T& old_value, const T& new_value );
// (1)
template< class ForwardIt, class T >
void replace( ForwardIt first, ForwardIt last,
const T& old_value, const T& new_value );
// (2)
template< class ExecutionPolicy, class ForwardIt, class T >
void replace( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
const T& old_value, const T& new_value );
// (1)
template< class ForwardIt, class T >
void replace( ForwardIt first, ForwardIt last,
const T& old_value, const T& new_value );
-
(1) Replaces all elements equal to
old_value
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_cvref_t<ExecutionPolicy>>
(od C++20) istrue
.
Parameters
first last | The range of elements to process. |
old_value | The value to search for and replace. |
new_value | The value to use as a replacement. |
policy | The execution policy to use. See execution policy for details. |
Type requirements
ForwardIt | LegacyForwardIterator |
- 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)
:
At most N comparisons with old_value
using operator==
.
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 (1)
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;
}
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
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
).
#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.