array::rbegin() and array::rend() in C++ STL
Last Updated :
28 Jul, 2022
- 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
#include <bits/stdc++.h>
using namespace std;
int main()
{
array< int , 5> arr = { 1, 5, 2, 4, 7 };
cout << "The last element is " << *(arr.rbegin()) << endl;
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
Time Complexity: O(N) where N is the size of the array.
Auxiliary Space: O(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
#include <bits/stdc++.h>
using namespace std;
int main()
{
array< int , 5> arr = { 1, 5, 2, 4, 7 };
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
Time Complexity: O(N) where N is the size of the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...