C++ std::rotate_copy() Function

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

Definition

In C++, std::rotate_copy() writes the rotated version of a range to a destination. The source is left untouched.

The element at middle becomes the first element in the output.

Syntax

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

C++
std::rotate_copy(src.begin(), src.begin() + 2, src.end(), std::back_inserter(out));
std::rotate_copy(s.begin(), s.begin() + 1, s.end(), out_iter);
CallWhat it does
std::rotate_copy(src.begin(), src.begin() + 2, src.end(), std::back_inserter(out));Copy with a left rotation of two.
std::rotate_copy(s.begin(), s.begin() + 1, s.end(), out_iter);Send the rotated data to any output iterator.
Library declarations
C++
template<class ForwardIt, class OutputIt>
OutputIt rotate_copy(ForwardIt first, ForwardIt middle,
                     ForwardIt last, OutputIt d_first);

Parameters

ParameterDescription
first, lastRequired. The source range.
middleRequired. The element that becomes the first one in the output.
d_firstRequired. Where the rotated copy starts.

Return Value

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

C++ Examples

Example 1 — Rotate into a new vector

Keep the original and build a rotated copy.

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

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

    std::rotate_copy(src.begin(), src.begin() + 2, 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. The middle iterator points at 3, so the copy starts there.
  2. After the last element, the copy wraps around to 1 and 2.
  3. src is printed unchanged.

Example 2 — Start from a chosen day

List the days of the week starting from Wednesday.

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

int main() {
    std::vector<std::string> days = {"Mon", "Tue", "Wed", "Thu"};
    std::vector<std::string> out;

    std::rotate_copy(days.begin(), days.begin() + 2, days.end(), std::back_inserter(out));

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

How It Works

  1. Index 2 holds Wed, so it is chosen as the middle.
  2. Mon and Tue are placed after Thu, as the circle continues.
  3. The original days vector keeps its order.

Example 3 — Print a rotated string

Send the rotated text straight to the screen.

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

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

    std::rotate_copy(s.begin(), s.begin() + 1, s.end(),
                     std::ostream_iterator<char>(std::cout));
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::ostream_iterator<char> prints each character it receives.
  2. Rotating by one moves the first letter h to the end.
  3. No second string is needed.

Frequently Asked Questions

In C++, std::rotate_copy() copies a range into a destination as if it was rotated. 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.
rotate() changes the original range. rotate_copy() writes the rotated result to another place.
Yes. That is the easiest way to build the result in a new vector.

Next: sample()

Picks n random elements from a range.

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

15 people found this page helpful