The reverse() function in C++ STL is used to reverse the order of elements within a specified range. It works with arrays as well as STL containers such as vector, deque, string, and list (through iterators).
- Reverses elements directly in the original container or array.
- Works with any bidirectional iterator range.
- Defined in the <algorithm> header file.
- Performs the reversal in linear time without requiring an additional container.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
// Reversing the vector
reverse(v.begin(), v.end());
for (int i : v) cout << i << " ";
return 0;
}
Output
5 4 3 2 1
Explanation: The reverse() function reverses all elements in the vector by swapping elements from both ends of the range until the middle is reached. The vector {1, 2, 3, 4, 5} becomes {5, 4, 3, 2, 1}.
Syntax
The reverse() function is defined in the <algorithm> header file.
reverse(first, last);
Parameters:
- first: Iterator to the first element in the range.
- last: Iterator to the theoretical element just after the last element in the range.
Return Value: This function does not return any value. It reverses the range in-place.
Examples of reverse() function
The following examples demonstrate how to use the reverse() function with different data structures.
Reversing an Array
The reverse() function can be used to reverse all elements of an array by passing pointers to the beginning and end of the array.
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
// Reverse the array arr
reverse(arr, arr + n);
for (int i : arr) cout << i << " ";
return 0;
}
Output
5 4 3 2 1
Explanation: The range arr to arr + n represents the entire array. The reverse() function reverses the order of all elements, resulting in {5, 4, 3, 2, 1}.
Reverse a String
The reverse() function can also be used to reverse the characters of a string by specifying the range from the first character to the last character.
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "abcd";
// Reverse the string s
reverse(s.begin(), s.end());
cout << s;
return 0;
}
Output
dcba
Explanation: The reverse() function reverses all characters in the string. The string "abcd" becomes "dcba".
Left Rotate a Vector using reverse()
The reverse() function can be used to efficiently perform left rotation of a vector. The idea is to reverse the first d elements, then reverse the remaining elements, and finally reverse the entire vector.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 6, 2, 9};
int n = v.size();
int d = 2;
// Left rotate the vector by d place
reverse(v.begin(), v.begin() + d);
reverse(v.begin() + d, v.end());
reverse(v.begin(), v.end());
for (auto i : v)
cout << i << " ";
return 0;
}
Output
6 2 9 1 3
Explanation: The vector is left-rotated by reversing the first d elements, then the remaining elements, and finally the entire vector. For d = 2, the vector {1, 3, 6, 2, 9} becomes {6, 2, 9, 1, 3}.