C++ std::reverse_copy() Function

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

Definition

In C++, std::reverse_copy() reads a range from the end to the start and writes the elements to a destination. The source is not changed.

Use it when you want both the original and the reversed version.

Syntax

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

C++
std::reverse_copy(src.begin(), src.end(), std::back_inserter(out));
std::reverse_copy(a + 1, a + 4, b);
CallWhat it does
std::reverse_copy(src.begin(), src.end(), std::back_inserter(out));Append the reversed elements to a vector.
std::reverse_copy(a + 1, a + 4, b);Reverse part of an array into another array.
Library declarations
C++
template<class BidirIt, class OutputIt>
OutputIt reverse_copy(BidirIt first, BidirIt last, OutputIt d_first);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_firstRequired. Where the reversed copy starts.

Return Value

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

C++ Examples

Example 1 — Keep the original

Reverse a vector into a new one.

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

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

    std::reverse_copy(src.begin(), src.end(), std::back_inserter(out));

    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. reverse_copy() reads the source from the back.
  2. std::back_inserter appends each element to out.
  3. The source keeps its original order.

Example 2 — Palindrome check

A word is a palindrome if it reads the same backward.

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

int main() {
    std::string s = "madam";
    std::string rev;

    std::reverse_copy(s.begin(), s.end(), std::back_inserter(rev));

    std::cout << s << (rev == s ? " is a palindrome" : " is not a palindrome") << '\n';
    return 0;
}

How It Works

  1. The reversed text of madam is built in rev.
  2. Comparing rev == s checks whether both strings match.
  3. They match, so the first message prints.

Example 3 — Reverse part of an array

Copy a reversed slice into a smaller array.

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

int main() {
    int a[] = {1, 2, 3, 4, 5};
    int b[3];

    std::reverse_copy(a + 1, a + 4, b);

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

How It Works

  1. a + 1 and a + 4 select 2, 3 and 4.
  2. They are written into b in reverse order.
  3. The elements 1 and 5 are outside the range, so they are ignored.

Frequently Asked Questions

In C++, std::reverse_copy() copies a range into a destination in reverse order. 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.
reverse() changes the range in place. reverse_copy() writes the reversed elements to another place and keeps the source.
No. They must be separate ranges.

Next: rotate()

Rotates the elements of a range so a chosen element becomes the first.

rotate() 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.

6 people found this page helpful