6 ways to Reverse Array in C++

C-Plus-Plus-array

In this article, we are going to learn about 6 ways to Reverse Array in C++ . We will learn to do this by multiple techniques with example

1. Using Iterative approach


In this example, we are trying to reverse an array by just iterating over the array elements using for loop. We are doing the logic of reversing the array elements in a separate function reverse(). Finally, we are printing the final output using court

Using Iterative approach

#include <iostream>
using namespace std;
 
#define MAXARRAY 50

// Function to reverse an array
void reverse(int arr[], int n)
{
    int arrrev[MAXARRAY];
 
    for (int i = 0; i < n; i++) {
        arrrev[n - 1 - i] = arr[i];
    }
 
    for (int i = 0; i < n; i++) {
        arr[i] = arrrev[i];
    }
}
 
int main()
{
    int Testarr[] = { 0,1, 2,3,4,5,6,7,8,9 };
    int count = sizeof(Testarr)/sizeof(Testarr[0]);
 
    reverse(Testarr, count);

cout <<"The Reversed array is : ";
    for (int i = 0; i < count; i++) {
cout << Testarr[i] << ",";
}
 
    return 0;
}

Output

The Reversed array is : 9,8,7,6,5,4,3,2,1,0,

2. Using reccursive function call


In this example, we are trying to reverse an array by using the recursion. In this, we have written the logic in a reverse function and then inside this function, we are calling this same function recursively until the array is reverse.

#include <iostream>
#include <algorithm>
using namespace std;
 
void reverse(int arr[], int index, int count)
{
    if (index < count)
    {
        swap(arr[index], arr[count]);
        reverse(arr, index + 1, count - 1);
    }
}
 
int main()
{
    int arr[] = { 0,1,2,3,4,5,6,7,8,9 };
    int count = sizeof(arr)/sizeof(arr[0]);
 
    reverse(arr, 0, count-1);
cout <<"The Reversed array is : ";
for (int k = 0; k < count; k++) {
        cout << arr[k] << " ";
    }
 
    return 0;
}

Output

The Reversed array is : 9 8 7 6 5 4 3 2 1 0

3. STL standard Libaray reverse function


In this example, we are using the reverse function from the STL standard library of C++. This function is available in the algorithm.h header file. so let us jump to our example and see how we can reverse a C++ array using STL.

#include <iostream>
#include <algorithm>
using namespace std;
 

void reverse(int arr[], int n) {
    reverse(arr, arr + n);
}
 
int main()
{
    int arr[] = { 0,1,2,3,4,5,6,7,8,9 };
    int n = sizeof(arr)/sizeof(arr[0]);
 
    reverse(arr, n);
cout <<"The Reversed array is : ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
 
    return 0;
}

Output

The Reversed array is : 9 8 7 6 5 4 3 2 1 0

4. STL iterators


In this example, we are going to make use of STL iterators. We will also use the Vector array instead of a simple C++ array. The vectors can be iterated from beginning to end and end to begin. So we will Use rbegin & rend Iterators to reverse a Vector Array in C++.

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;


int main()
{
    std::vector<int> testarr{0,1,2,3,4,5,6,7,8,9 };

cout <<"The Original array is : ";
for (auto it = testarr.begin(); it != testarr.end(); it++)
    {
        // Print each element
        std::cout << *it << ",";
    }
    std::cout << std::endl;
cout <<"The Reversed array is : ";
for (auto it = testarr.rbegin(); it != testarr.rend(); it++)
    {
        // Print each element
        std::cout << *it << ",";
    }
    std::cout << std::endl;

    return 0;
}

Output

The Original array is : 0,1,2,3,4,5,6,7,8,9,
The Reversed array is : 9,8,7,6,5,4,3,2,1,0,

5. Vector array


In this example, we are creating a vector array, and then using this array we are creating another array with the values reversed. let us see with the help of code how this is working.

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
    vector<int> arrtest{ 0,1,2,3,4,5,6,7,8,9 };

    vector<int> revarr(arrtest.rbegin(), arrtest.rend());
   
    cout <<"The Reversed array is : ";
for (auto it = revarr.begin(); it != revarr.end(); it++)
    {
        // Print each element
        std::cout << *it << ",";
    }
    std::cout << std::endl;


    return 0;
}

Output

The Reversed array is : 9,8,7,6,5,4,3,2,1,0,

6. STL reverse function


In this example, we are using the STL reverse function on the vector array. First, we are declaring a vector array and then we are applying the reverse function on this array.

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> arrtest = { 0,1,2,3,4,5,6,7,8,9 };

    std::reverse(arrtest.begin(), arrtest.end());
    cout <<"The Reversed array is : ";
for (auto it = arrtest.begin(); it != arrtest.end(); it++)
    {
        // Print each element
        std::cout << *it << ",";
    }
    std::cout << std::endl;

    return 0;
}

Output

The Reversed array is : 9,8,7,6,5,4,3,2,1,0,

Summary

In this article, we have learned 6 ways to Reverse Array in C++ with examples