replace


Replace a specified value in a sequence with another value.

Replace every occurrence of old_value in the range [ first ... last ) with new_value .

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class ForwardIterator, class T >
void replace
  (
  ForwardIterator first,
  ForwardIterator last,
  const T& old_value,
  const T& new_value
  );
  

Complexity

Time complexity is linear, as ( last - first ) comparisons are performed. Space complexity is constant.

Example <ospace/osstd/examples/replace0.cpp>
#include <iostream>
#include <algorithm>

int numbers[ 6 ] = { 0, 1, 2, 0, 1, 2 };

void
main()
  {
  replace( numbers, numbers + 6, 2, 42 );

  for ( int i = 0; i < 6; ++i )
    cout << numbers[ i ] << ` `;
  cout << "\n";
  }

0 1 42 0 1 42

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.