C++ std::replace_copy() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::replace_copy() copies a range into a destination and swaps every old value for a new value on the way. The source is not changed.

It is the safe version of std::replace() when you still need the original data.

Syntax

Add #include <algorithm>, then call std::replace_copy(). Start with these two lines.

C++
std::replace_copy(src.begin(), src.end(), out.begin(), 2, 20);
std::replace_copy(src.begin(), src.end(), std::back_inserter(out), old, now);
CallWhat it does
std::replace_copy(src.begin(), src.end(), out.begin(), 2, 20);Copy while changing each 2 to 20.
std::replace_copy(src.begin(), src.end(), std::back_inserter(out), old, now);Append the result to a growing vector.
Library declarations
C++
template<class InputIt, class OutputIt, class T>
OutputIt replace_copy(InputIt first, InputIt last, OutputIt d_first,
                      const T& old_value, const T& new_value);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_firstRequired. Where the copy starts.
old_valueRequired. The value to replace.
new_valueRequired. The value to write instead.

Return Value

Return ValueDescription
IteratorPoints just after the last element written in the destination.

C++ Examples

Example 1 — Keep the original

Change 2 to 20 in a copy and show both vectors.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> src = {1, 2, 3, 2};
    std::vector<int> out(4);

    std::replace_copy(src.begin(), src.end(), out.begin(), 2, 20);

    std::cout << "src: ";
    for (int x : src) std::cout << x << ' ';
    std::cout << '\n';
    std::cout << "out: ";
    for (int x : out) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. out is created with four slots to receive the copy.
  2. Each value is copied, but 2 is replaced by 20 on the way.
  3. The printed src is unchanged, which is the main point.

Example 2 — Replace words

Swap one word for another in a new vector.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> words = {"red", "blue", "red"};
    std::vector<std::string> out;

    std::replace_copy(words.begin(), words.end(), std::back_inserter(out),
                      std::string("red"), std::string("green"));

    for (const std::string& x : out) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. Both values are std::string, so they have the same type.
  2. std::back_inserter grows out for each copied word.
  3. Only the red entries are replaced.

Example 3 — Change characters

Turn dashes into spaces in a new string.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

int main() {
    std::string text = "a-b-c";
    std::string result;

    std::replace_copy(text.begin(), text.end(), std::back_inserter(result), '-', ' ');

    std::cout << result << '\n';
    std::cout << text << '\n';
    return 0;
}

How It Works

  1. result starts empty and grows with std::back_inserter.
  2. Each '-' is written as a space in the new string.
  3. The original text is printed last and still has its dashes.

Frequently Asked Questions

In C++, std::replace_copy() copies a range while replacing one value with another. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the position after the last element written in the destination.
replace() changes the original range. replace_copy() leaves it alone and writes the changed values elsewhere.
No. Make sure the destination has room, or use std::back_inserter.

Next: replace_copy_if()

Copies a range while replacing the elements that satisfy a condition.

replace_copy_if() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

23 people found this page helpful