Open In App

rotate() in C++ STL

Last Updated : 06 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
3 4 5 1 2 

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:

  • first: Iterator to the first element in the range.
  • mid: Iterator to the element that becomes the new first element.
  • last: Iterator to the theoretical element just after the last element in the range.

Return Value:

  • This function does not return any value. It rotates the range in-place.

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;
}

Output
3 4 5 1 2 

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;
}

Output
4 5 1 2 3 

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;
}

Output
3 4 5 1 2 


Next Article
Article Tags :
Practice Tags :

Similar Reads