Przejdź do głównej zawartości

std::replace() algorithm

// (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) Replaces all elements equal to old_value 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_cvref_t<ExecutionPolicy>>  (od C++20) is true.

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

ForwardItLegacyForwardIterator
*firstMust be writable to d_first.

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 other ExecutionPolicy, 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).

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() algorithm

// (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) Replaces all elements equal to old_value 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_cvref_t<ExecutionPolicy>>  (od C++20) is true.

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

ForwardItLegacyForwardIterator
*firstMust be writable to d_first.

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 other ExecutionPolicy, 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).

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.