Home C++ Algorithms Functions rotate() C++ std::rotate() Function Definition In C++, std::rotate() moves the elements of a range in a circle. The element you pick as middle becomes the new first element, and the earlier ones wrap around to the end.
The order of elements stays the same around the circle. It is useful for shifting data without losing any.
Syntax Add #include <algorithm>, then call std::rotate(). Start with these two lines.
std::rotate(v.begin(), v.begin() + 2, v.end());
std::rotate(v.begin(), v.end() - 1, v.end()); Call What it does std::rotate(v.begin(), v.begin() + 2, v.end());Rotate left by two places. std::rotate(v.begin(), v.end() - 1, v.end());Rotate right by one place.
Library declarations template<class ForwardIt>
ForwardIt rotate(ForwardIt first, ForwardIt middle, ForwardIt last); Parameters Parameter Description first, last Required. The range to rotate. middle Required. The element that becomes the first element after the rotation.
Return Value Return Value Description Iterator Points at the element that was first before the rotation.
C++ Examples 1. Rotate left 2. Rotate right 3. Use the return value Example 1 — Rotate left Make the third element the new first one.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::rotate(v.begin(), v.begin() + 2, v.end());
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works v.begin() + 2 points at 3, so 3 becomes the new first element.The two elements before it, 1 and 2, wrap around to the end. No values are lost, only moved. Example 2 — Rotate right Move the last element to the front.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::rotate(v.begin(), v.end() - 1, v.end());
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works v.end() - 1 points at the last element, 4.Choosing it as middle brings 4 to the front. The other three elements shift one place right. Example 3 — Use the return value Find where the old first character moved.
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string s = "abcdef";
auto it = std::rotate(s.begin(), s.begin() + 2, s.end());
std::cout << s << '\n';
std::cout << "'a' moved to index " << (it - s.begin()) << '\n';
return 0;
} cdefab
'a' moved to index 4 How It Works s.begin() + 2 points at c, so the text starts with c after the call.The returned iterator marks the new place of the old first character a. It sits at index 4, right after cdef. Frequently Asked Questions What does std::rotate() do? In C++, std::rotate() rotates the elements of a range so a chosen element becomes the first. Include <algorithm> to use it.
Which header and C++ version do I need for std::rotate()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::rotate() return? It returns an iterator to the position where the old first element ended up.
How do I rotate a vector left by n places? Call std::rotate(v.begin(), v.begin() + n, v.end()).
How do I rotate right by n places? Call std::rotate(v.begin(), v.end() - n, v.end()).
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.
16 people found this page helpful
Helpful Share Copy link Suggestion