reverse() in C++ STL Last Updated : 06 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 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; } Output5 4 3 2 1 This article covers the syntax, usage, and common examples of reverse() function in C++:Table of ContentSyntax of reverse()Examples of reverse()Reverse an ArrayReverse a StringLeft Rotate a Vector using reverse()Syntax of reverse()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()The below examples show how to use the reverse() function to reverse variety of data containers.Reverse 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]); // Reverse the array arr reverse(arr, arr + n); for (int i : arr) cout << i << " "; return 0; } Output5 4 3 2 1 Reverse a String C++ #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; } OutputdcbaLeft Rotate a Vector using reverse()The left rotation of a vector can be done by using reverse() three times on it. C++ #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; } Output6 2 9 1 3 Comment More infoAdvertise with us Next Article reverse() in C++ STL H Hardik Gaur Improve Article Tags : C++ STL cpp-algorithm-library Practice Tags : CPPSTL Similar Reads std :: reverse_copy in C++ STL C++ STL provides a function that copies the elements from the given range but in reverse order. Below is a simple program to show the working of reverse_copy(). Examples: Input : 1 2 3 4 5 6 7 8 9 10 Output : The vector is: 10 9 8 7 6 5 4 3 2 1 The function takes three parameters. The first two are 2 min read forward_list::reverse() in C++ STL std::forward_list::reverse() is an inbuilt function in CPP STL which reverses the order of the elements present in the forward_list. Syntax: forwardlist_name.reverse()Parameter: The function does not accept any parameter. Return value: The function has no return value. It reverses the forward list. 1 min read rotate() in C++ STL 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 mai 4 min read C++ STL - Vector in Reverse Order Prerequisite: Vectors in C++ A vector can be printed in reverse order with the following methods: By traversing in the backward direction using indexingBy traversing in the backward direction using begin() and end() functions in C++ STLBy traversing in the backward direction using rbegin() and rend( 3 min read list reverse function in C++ STL The list::reverse() is a built-in function in C++ STL which is used to reverse a list container. It reverses the order of elements in the list container. Syntax: list_name.reverse()Parameters: This function does not accept any parameters. Return Value: This function does not return any value. It jus 1 min read Like