Home »
C++ STL
array::rbegin() and array::rend() functions with Example in C++ STL
C++ STL | array::rbegin() and array::rend() functions: Here, we are going to learn about the array::rbegin() and array::rend() functions of Array in C++ STL.
Submitted by IncludeHelp, on March 01, 2019
C++ STL array::rbegin() and array::rend() functions
array::rbegin() function is a library function of array and it is used to get the first element (from reverse side) of the array, it returns a reverse iterator pointing to the last element of the array.
array::rend() function is a library function of array and it is used to get the last element (from reverse side i.e. first element) of the array, it returns a reverse iterator pointing to the last element of the array.
Syntax
array::rbegin();
array::rend();
Parameter(s)
None
Return value
The function returns reverse iterators pointing to the first and last elements of an array.
Sample Input and Output
Input or array declaration:
array<int,5> arr {10, 20, 30, 40, 50};
Function call:
auto it=arr.rbegin();
cout<<*it;
it=arr.rend();
cout<<*it;
Output:
50 10
Example
C++ STL program to demonstrate example of array::rbegin() and array::rend() functions:
#include <array>
#include <iostream>
using namespace std;
int main() {
array<int, 5> numbers{10, 20, 30, 40, 50};
array<string, 5> cities{"New Delhi", "Mumbai", "Gwalior"};
cout << "Elements of numbers array..." << endl;
for (auto it = numbers.rbegin(); it != numbers.rend(); it++)
cout << *it << " ";
cout << endl;
cout << "Elements of cities array..." << endl;
for (auto it = cities.rbegin(); it != cities.rend(); it++) cout << *it << " ";
cout << endl;
return 0;
}
Output
Elements of numbers array...
50 40 30 20 10
Elements of cities array...
Gwalior Mumbai New Delhi