A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/rotate-in-cpp-stl/ below:

rotate() in C++ STL - GeeksforGeeks

rotate() in C++ STL

Last Updated : 23 Jul, 2025

In C++, rotate() is a built-in function used to rotate the elements of a range in left or right direction such that the element pointed to by a specified iterator becomes the new first element of the range.

Let's take a look at an example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3, 4, 5};

    // Rotating vector 2 places to right such that
  	// element at index 2 becomes first element
    rotate(v.begin(), v.begin() + 2, v.end());

    for (int i : v) cout << i << " ";
    return 0;
}

Explanation: The rotate() function rotates the range so that the specified element (v.begin() + 2 here) becomes the first element. Rotation can be clockwise (right) or anticlockwise.

This article covers the syntax, usage, and common examples of the rotate() function in C++.

Syntax of rotate()

The rotate() function is defined in the <algorithm> header file.

rotate(first, mid, last);

Parameters:

Return Value:

Types of Rotation

The rotation operation can be done in two ways:

Right Rotation

Right rotation of a vector involves shifting all elements to the right by a specified number of positions. The elements shifted out from the right end are wrapped around to the left end of the vector.

Left Rotation

Left rotation of a vector means shifting all the elements of the vector to the left by a specified number of positions. The elements that are shifted out from the left end are wrapped around to the right end of the vector.

Examples of rotate()

The examples below demonstrate how to use the rotate() function on different types of data containers for left rotation (anticlockwise) and right rotation (clockwise).

Left Rotate an Array C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    int d = 2;

    // Left rotate the arr by d place
    rotate(arr, arr + d, arr + n);

    for (auto i : arr) cout << i << " ";
    return 0;
}
Right Rotate an Array C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    int d = 2;

    // Right rotate the arr by d place
    rotate(arr, arr + n - d, arr + n);

    for (auto i : arr) cout << i << " ";
    return 0;
}
Left Rotate a List C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    list<int> l = {1, 2, 3, 4, 5};

    // Rotate l to places to the Left
    rotate(l.begin(), next(l.begin(), 2), l.end());

    for (int i : l) cout << i << " ";
    return 0;
}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4