
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to reverse an Array using STL in C++?
Here we will see how to reverse an array using STL functions in C++. So if the array is like A = [10, 20, 30, 40, 50, 60], then the output will be B = [60, 50, 40, 30, 20, 10]. To reverse we have one function called reverse() present in the header file <algorithm>. The code is like below −
Example
#include <iostream> #include <algorithm> using namespace std; int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Array before reverse: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; reverse(arr, arr + n); cout << "\nArray after reverse: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; }
Output
Array before reverse: 10 20 30 40 50 60 Array after reverse: 60 50 40 30 20 10
- Related Articles
- All reverse permutations of an array using STL in C++?
- How to sort an Array using STL in C++?
- How to reverse a Vector using STL in C++?
- Reverse an array using C#
- Shuffle an Array using STL in C++
- Sorting an array according to another array using pair in STL in C++
- How to find the maximum element of an Array using STL in C++?
- forward_list::reverse( ) in C++ STL
- Reverse an array in C++
- How to find the sum of elements of an Array using STL in C++?
- C# program to reverse an array
- Array product in C++ using STL
- How to find the minimum and maximum element of an Array using STL in C++?
- List reverse function in C++ STL
- How to reverse the elements of an array using stack in java?

Advertisements