std::reverse() in C++

17 Feb, 2021
reverse() is a predefined function in header file algorithm. It is defined as a template in the above mentioned header file. It reverses the order of the elements in the range [first, last) of any container. The time complexity is O(n). Note: The range used is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.Syntax: 
void reverse(BidirectionalIterator first, BidirectionalIterator last)
BidirectionalIterator is an iterator that can be used to access any
elements of a container in both forward and backward direction.
Examples: 
Input : 10 11 12 13 14 15 16 17
Output :10 11 12 13 14 17 16 15
Explanation:
reverse(v.begin() + 5, v.begin() + 8);
In the above function, input we have applied reverse() on the vector
from index 5 to index 7.
Therefore when we display the vector we get reverse order
from index 5 to index 7.
CPP
// CPP program to illustrate// std::reverse() function of STL#include <algorithm>#include <iostream>#include <vector>using namespace std;  int main(){    vector<int> vec1;    vector<int>::iterator p;      // Inserting elements in vector    for (int i = 0; i < 8; i++) {        vec1.push_back(i + 10);    }    // Displaying elements of vector    cout<<"Initial Vector:"<<endl;    for(p = vec1.begin(); p < vec1.end(); p++) {        cout << *p << " ";    }    cout << endl;      cout << "Reverse only from index 5 to 7 in vector:\n";    // Reversing elements from index 5 to index 7    reverse(vec1.begin() + 5, vec1.begin() + 8);      // Displaying elements of vector after Reversing    for (p = vec1.begin(); p < vec1.end(); p++) {        cout << *p << " ";    }    cout << endl <<endl;      vector<int> vec2{ 4, 5, 6, 7 };      cout<<"Initial Vector:"<<endl;    for (p = vec2.begin(); p < vec2.end(); p++) {        cout << *p << " ";    }    cout << endl;      cout << "Reverse full Vector:"<<endl;    // Reversing directly from beginning to end    reverse(vec2.begin(), vec2.end());          // Displaying elements of vector after Reversing    for (p = vec2.begin(); p < vec2.end(); p++) {        cout << *p << " ";    }    cout << endl;      return 0;}
Output: 
Initial Vector:
10 11 12 13 14 15 16 17 
Reverse only from index 5 to 7 in vector:
10 11 12 13 14 17 16 15 

Initial Vector:
4 5 6 7 
Reverse full Vector:
7 6 5 4 
Time complexity: O(n)Reverse in STL | Sample Video for C++ Productivity Hacks | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersReverse in STL | Sample Video for C++ Productivity Hacks | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:32•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=-NK9CQTdceg" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Hardik Gaur. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
shobitham1
sunnychaudharyvlsi
cpp-algorithm-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bitwise Operators in C/C++
Set in C++ Standard Template Library (STL)
Inheritance in C++
unordered_map in C++ STL
vector erase() and clear() in C++
C++ Classes and Objects
Substring in C++
Object Oriented Programming in C++
Priority Queue in C++ Standard Template Library (STL)
Sorting a vector in C++