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.
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.
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.
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.
Reverse only from index 5 to 7 in array: 10 11 12 13 14 17 16 15 Reverse full array: 7654
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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Draw an ellipse divided by straight line into two colored part in C++ Graphics
- What happens if we mix new and free in C++?
- VS Code | Build, Run and Debug in C++
- VS Code | Compile and Run in C++
- Difference between sizeof(int *) and sizeof(int) in C/C++
- Runtime and Compile-time constants in C++
- Efficient ways to compare a variable with multiple values
- Overloading the Comma Operator
- Difference between C++ and PHP
- Custom Jumble Word Game
- Comparator Class in C++ with Examples
- Complete Guide to Install C++17 in Windows
- Features of C++
- Draw a triangle in C++ graphics
- Implementation of lower_bound() and upper_bound() in List of Pairs in C++