Reverse Array in C++

  1. Use vector Range Based Constructor to Reverse Array in C++
  2. Use the std::reverse Function to Reverse Array in C++
  3. Use rbegin/rend Iterators to Reverse Array in C++

This article will explain several methods of how to reverse an array in C++.

Use vector Range Based Constructor to Reverse Array in C++

vector container supports constructor with the range specified by iterators. Hence, we can declare a new vector variable and initialize it with reversed values from the first vector using rbegin/rend iterators.

Note that we also declare a function PrintVector to keep the clone tidy and output vector contents in a single function call. One downside of this method is that it requires constructing a new array variable, which can be unnecessary overhead in certain scenarios.

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

using std::cout; using std::endl;
using std::vector; using std::copy;
using std::ostream_iterator;

void PrintVector(vector<int> &arr)
{
    copy(arr.begin(), arr.end(),
         ostream_iterator<int>(cout,"; "));
    cout << endl;
}

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

    vector<int> arr1_reversed(arr1.rbegin(), arr1.rend());
    PrintVector(arr1_reversed);

    return EXIT_SUCCESS;
}

Output:

10; 9; 8; 7; 6; 5; 4; 3; 2; 1;

Use the std::reverse Function to Reverse Array in C++

Alternatively, to reverse the array elements in place without declaring other variables, we can call the std::reverse function from the standard library. std::reverse is part of the <algorithm> header and has been part of the standard library since the C++17. The function takes start/end iterators of the range as function arguments and swaps the elements in place. The vector on which std::reverse has been called is modified permanently, and any consequent access of its elements results in a new ordering.

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

using std::cout; using std::endl;
using std::vector; using std::copy;
using std::ostream_iterator;
using std::reverse;

void PrintVector(vector<int> &arr)
{
    copy(arr.begin(), arr.end(),
         ostream_iterator<int>(cout,"; "));
    cout << endl;
}

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

    reverse(arr1.begin(), arr1.end());
    PrintVector(arr1);

    return EXIT_SUCCESS;
}

Output:

10; 9; 8; 7; 6; 5; 4; 3; 2; 1;

Use rbegin/rend Iterators to Reverse Array in C++

In contrast with the first example of this topic, there are use cases when the reordered contents of the vector need not be stored in the program flow, rather just outputted to console or display. The following example demonstrates how to print the array elements to console in reverse order and without modifying underlying variable contents.

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

using std::cout; using std::endl;
using std::vector; using std::copy;
using std::ostream_iterator;

void PrintVector(vector<int> &arr)
{
    copy(arr.begin(), arr.end(),
         ostream_iterator<int>(cout,"; "));
    cout << endl;
}

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

    copy(arr1.rbegin(), arr1.rend(),
         ostream_iterator<int>(cout,"; "));
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

10; 9; 8; 7; 6; 5; 4; 3; 2; 1;
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Array

  • Find Array Size in C++
  • Print an Array in C++