Print contents of an array in reverse order in C++
This post will discuss how to print the contents of an array in reverse order in C++.
1. Using Array Indices
A naive solution is to loop through the array elements and print each element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> // Print contents of an array in reverse order in C++ // using array indices int main() { int arr[] = { 10, 20, 30, 40 }; size_t n = sizeof(arr)/sizeof(arr[0]); // iterate backward over the elements of an array for (int i = n - 1; i >= 0; i--) { std::cout << arr[i] << ' '; } return 0; } |
Output:
40 30 20 10
The above code uses the sizeof operator for determining the array size. We can also create a template that deduces the size of the array from its declared type.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> template<typename T, int n> void print_array(T const(& arr)[n]) { for (int i = n - 1; i >= 0; i--) { std::cout << arr[i] << ' '; } } // Print contents of an array in C++ using templates int main() { int arr[] = { 10, 20, 30, 40 }; print_array(arr); return 0; } |
Output:
40 30 20 10
2. Using std::copy function
Another good alternative is to use the output iterator std::ostream_iterator to print array contents to the output stream std::cout. We can do it with the help of std::copy, which takes the reverse iterator to the starting and ending positions of the array and the output iterator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <algorithm> #include <iterator> // Print contents of an array in reverse order in C++ using `std::ostream_iterator` int main() { int arr[] = { 10, 20, 30, 40 }; int n = sizeof(arr)/sizeof(arr[0]); std::reverse_iterator<int*> begin(arr + n); std::reverse_iterator<int*> end(arr); std::copy(begin, end, std::ostream_iterator<short>(std::cout, " ")); return 0; } |
Output:
40 30 20 10
With C++17, we can use std::copy with std::experimental::ostream_joiner which is defined in header <experimental/iterator>. It is a single-pass output iterator which can write successive array elements into the std::cout, using the << operator, separated by a delimiter between every two elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <experimental/iterator> int main() { int input[] = { 1, 2, 3, 4, 5 }; std::copy(std::rbegin(input), std::rend(input), std::experimental::make_ostream_joiner(std::cout, " ")); return 0; } |
Output:
5 4 3 2 1
3. Using Iterators
We can get iterators to the array with the help of std::cbegin and std::cend, which are introduced in C++11. The idea is to start a loop from std::cend, which returns a constant iterator to the array’s end. Then we iterate backward and print each element till we reach std::start, which returns a constant iterator to the beginning of the array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> // Print contents of an array in reverse order in C++ // using iterators int main() { int arr[] = { 10, 20, 30, 40 }; auto it = std::cend(arr); while (it != std::cbegin(arr)) { std::cout << *(--it) << ' '; } return 0; } |
Output:
40 30 20 10
4. Using std::for_each function
We can also use std::for_each that takes an input range defined by two iterators and applies a function on every element of that range. The function can be a unary function or an object of a class overloading the ()operator or a lambda expression.
Function
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <vector> #include <algorithm> void print(const int &i) { std::cout << i << ' '; } // Print contents of an array in reverse order in C++ // using `std::for_each` int main() { int arr[] = { 1, 2, 3, 4, 5 }; int n = sizeof(arr)/sizeof(arr[0]); std::reverse_iterator<int*> begin(arr + n); std::reverse_iterator<int*> end(arr); std::for_each(begin, end, print); return 0; } |
Class
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <vector> #include <algorithm> struct myclass { void operator() (int i) { std::cout << i << ' '; } } ob; // Print contents of an array in reverse order in C++ // using `std::for_each` int main() { int arr[] = { 1, 2, 3, 4, 5 }; int n = sizeof(arr)/sizeof(arr[0]); std::reverse_iterator<int*> begin(arr + n); std::reverse_iterator<int*> end(arr); std::for_each(begin, end, ob); return 0; } |
Lambda
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <vector> #include <algorithm> // Print contents of an array in reverse order in C++ // using `std::for_each` int main() { int arr[] = { 1, 2, 3, 4, 5 }; int n = sizeof(arr)/sizeof(arr[0]); std::reverse_iterator<int*> begin(arr + n); std::reverse_iterator<int*> end(arr); std::for_each(begin, end, [](const int &e) { std::cout << e << " "; }); return 0; } |
Output:
5 4 3 2 1
That’s all about printing the contents of an array in reverse order in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)