C++ std::rotate() Function

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

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.

C++
std::rotate(v.begin(), v.begin() + 2, v.end());
std::rotate(v.begin(), v.end() - 1, v.end());
CallWhat 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
C++
template<class ForwardIt>
ForwardIt rotate(ForwardIt first, ForwardIt middle, ForwardIt last);

Parameters

ParameterDescription
first, lastRequired. The range to rotate.
middleRequired. The element that becomes the first element after the rotation.

Return Value

Return ValueDescription
IteratorPoints at the element that was first before the rotation.

C++ Examples

Example 1 — Rotate left

Make the third element the new first one.

C++
#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

  1. v.begin() + 2 points at 3, so 3 becomes the new first element.
  2. The two elements before it, 1 and 2, wrap around to the end.
  3. No values are lost, only moved.

Example 2 — Rotate right

Move the last element to the front.

C++
#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

  1. v.end() - 1 points at the last element, 4.
  2. Choosing it as middle brings 4 to the front.
  3. The other three elements shift one place right.

Example 3 — Use the return value

Find where the old first character moved.

C++
#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;
}

How It Works

  1. s.begin() + 2 points at c, so the text starts with c after the call.
  2. The returned iterator marks the new place of the old first character a.
  3. It sits at index 4, right after cdef.

Frequently Asked Questions

In C++, std::rotate() rotates the elements of a range so a chosen element becomes the first. 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 where the old first element ended up.
Call std::rotate(v.begin(), v.begin() + n, v.end()).
Call std::rotate(v.begin(), v.end() - n, v.end()).

Next: rotate_copy()

Copies a range into a destination as if it was rotated.

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

16 people found this page helpful