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:
- 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;
}
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;
}
Similar Reads
reverse() in C++ STL In C++, the reverse() is a built-in function used to reverse the order of elements in the given range of elements. This range can be any STL container or an array. In this article, we will learn about reverse() function in C++.Letâs take a look at an example:C++#include <bits/stdc++.h> using n
3 min read
std::rotate vs std::rotate_copy in C++ STL rotate in STL:It rotates the order of the elements in the range [first, last), in such a way that the element pointed by middle becomes the new first element, i, e, to the left. CPP // Illustrating the use of rotate algorithm #include <bits/stdc++.h> using namespace std; // Driver Program int
2 min read
acos() function in C++ STL acos() is an inbuilt function in C++ STL and itâs the same as the inverse of cosine in maths. The acos() function returns the values in the range of [0, Ï] which is the angle in radians. Syntaxacos(data_type x);ParametersThis function accepts one mandatory parameter x which specifies the value whose
3 min read
transform() in C++ STL In C++, transform() is a built-in STL function used to apply the given operation to a range of elements and store the result in another range. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i
4 min read
atan2() function in C++ STL The atan2() is an inbuilt function in C++ STL which returns tangent inverse of (y/x), where y is the proportion of the y-coordinate and x is the proportion of the x-coordinate. The numeric value lies between -\pi and \pi representing the angle \theta of a (x, y) point and positive x-axis. It is the
3 min read