array::rbegin() and array::rend() in C++ STL
- array::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container.
Syntax:
array_name.rbegin()
Parameters: The function does not accept any parameter.
Return value: The function returns a reverse iterator pointing to the last element in the container.
Program to demonstrate the array::rbegin() method:
Program 1:
// CPP program to illustrate
// the array::rbegin() function
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// array initialisation
array<
int
, 5> arr = { 1, 5, 2, 4, 7 };
// prints the last element
cout <<
"The last element is "
<< *(arr.rbegin()) << endl;
// prints all the elements
cout <<
"The array elements in reverse order are:\n"
;
for
(
auto
it = arr.rbegin(); it != arr.rend(); it++)
cout << *it <<
" "
;
return
0;
}
Output:The last element is 7 The array elements in reverse order are: 7 4 2 5 1
- array::rend() is a built-in function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first element in the array container.
Syntax:
array_name.rend()
Parameters: The function does not take any parameter.
Return value: The function returns a reverse iterator pointing to the theoretical element right before the first element in the array container.
Program to demonstrate the array::rend() method:
Program 1:
// CPP program to illustrate
// the array::rend() function
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
array<
int
, 5> arr = { 1, 5, 2, 4, 7 };
// prints all the elements
cout <<
"The array elements in reverse order are:\n"
;
for
(
auto
it = arr.rbegin(); it != arr.rend(); it++)
cout << *it <<
" "
;
return
0;
}
Output:The array elements in reverse order are: 7 4 2 5 1