How to Reverse Array in C++
-
Use
vector
Range Based Constructor to Reverse Array in C++ -
Use the
std::reverse
Function to Reverse Array in C++ -
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 <iterator>
#include <vector>
using std::copy;
using std::cout;
using std::endl;
using std::ostream_iterator;
using std::vector;
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 <iterator>
#include <vector>
using std::copy;
using std::cout;
using std::endl;
using std::ostream_iterator;
using std::reverse;
using std::vector;
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 <iterator>
#include <vector>
using std::copy;
using std::cout;
using std::endl;
using std::ostream_iterator;
using std::vector;
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;
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook