reverse() in C++ STL Last Updated : 27 Sep, 2025 Comments Improve Suggest changes 49 Likes 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 like vector or can be an array. 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 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.ExamplesReversing an ArrayThe below examples show how to use the reverse() function to reverse variety of data containers. 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 Create Quiz reverse() in C++ STL Visit Course Comment H Hardik Gaur 49 Improve H Hardik Gaur 49 Improve Article Tags : C++ STL cpp-algorithm-library Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like