Given an array arr[], reverse this array using STL in C++.
Example:
Input: arr[] = {1, 45, 54, 71, 76, 12}
Output: {12, 76, 71, 54, 45, 1}
Input: arr[] = {1, 7, 5, 4, 6, 12}
Output: {12, 6, 4, 5, 7, 1}
Approach: Reversing can be done with the help of reverse() function provided in STL.
Syntax:
reverse(start_index, last_index);
// C++ program to reverse Array // using reverse() in STL #include <algorithm> #include <iostream> using namespace std; int main() { // Get the array int arr[] = { 1, 45, 54, 71, 76, 12 }; // Compute the sizes int n = sizeof(arr) / sizeof(arr[0]); // Print the array cout << "Array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; // Reverse the array reverse(arr, arr + n); // Print the reversed array cout << "\nReversed Array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; } |
chevron_right
filter_none
Output:
Array: 1 45 54 71 76 12 Reversed Array: 12 76 71 54 45 1
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- All reverse permutations of an array using STL in C++
- How to reverse a Vector using STL in C++?
- forward_list::reverse() in C++ STL
- list reverse function in C++ STL
- How to traverse a STL map in reverse direction?
- Sorting an array according to another array using pair in STL
- Count number of unique Triangles using STL | Set 1 (Using set)
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Count the number of 1's and 0's in a binary array using STL in C++ ?
- Array product in C++ using STL
- Finding Floor and Ceil of a Sorted Array using C++ STL
- Sort a nearly sorted array using STL
- Finding Median of unsorted Array in linear time using C++ STL
- Working with Array and Vectors using STL in C++
- How to find the sum of elements of an Array using STL in C++?
- How to sort an Array in descending order using STL in C++?
- How to find the maximum element of an Array using STL in C++?
- How to sort an Array using STL in C++?
- Program to check if an Array is Palindrome or not using STL in C++
- All permutations of an array using STL in C++
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : gufideg

