reverse an array in c++ stl
To reverse an array in C++ using the Standard Template Library (STL), you can make use of the std::reverse
function. Here are the steps to reverse an array using the STL in C++:
- Include the necessary headers:
#include <algorithm>
#include <iostream>
- Declare and initialize the array:
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
- Use the
std::reverse
function to reverse the array:
std::reverse(arr, arr + n);
- Print the reversed array:
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
This will output the reversed array: 5 4 3 2 1
.
Let me know if you have any further questions.