C++ std::rotate_copy() Function

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.
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);
| Call | What 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
template<class ForwardIt, class OutputIt>
OutputIt rotate_copy(ForwardIt first, ForwardIt middle,
ForwardIt last, OutputIt d_first);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The source range. |
| middle | Required. The element that becomes the first one in the output. |
| d_first | Required. Where the rotated copy starts. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points 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.
#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;
}
src: 1 2 3 4 5
out: 3 4 5 1 2
How It Works
- The middle iterator points at 3, so the copy starts there.
- After the last element, the copy wraps around to 1 and 2.
src is printed unchanged.
Example 2 — Start from a chosen day
List the days of the week starting from Wednesday.
#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
- Index 2 holds
Wed, so it is chosen as the middle. - Mon and Tue are placed after Thu, as the circle continues.
- The original
days vector keeps its order.
Example 3 — Print a rotated string
Send the rotated text straight to the screen.
#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
std::ostream_iterator<char> prints each character it receives.- Rotating by one moves the first letter
h to the end. - 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.
About the author
Developer, cloud engineer, and technical writer
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