replace_copy


Copy a sequence, replacing matching values.

Copy the sequence [ first ... last ) to a sequence of the same size starting at result , replacing all occurrences of old_value with new_value . Return an iterator positioned immediately after the last new element.

Library

Standards<ToolKit>

Declaration


template< class InputIterator, class OutputIterator, class T >
OutputIterator replace_copy
  (
  InputIterator first,
  InputIterator last,
  OutputIterator result,
  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/replcpy1.cpp>
#include <iostream>
#include <algorithm>

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

int main()
  {
  replace_copy( numbers, numbers + 6, result, 2, 42 );

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

0 1 42 0 1 42

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