array::crbegin() and array::crend() in C++ STL
- array::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the last element in the container.
Syntax:
array_name.crbegin()
Parameters: The function does not accepts any parameter.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.Return value: The function returns a reverse iterator pointing to the last element in the container.
Program to demonstrate the array::crbegin() method:
Program 1:
// CPP program to illustrate// the array::crbegin() function#include <bits/stdc++.h>usingnamespacestd;intmain(){// array initialisationarray<int, 5> arr = { 1, 5, 2, 4, 7 };// prints the last elementcout <<"The last element is "<< *(arr.crbegin()) << endl;// prints all the elementscout <<"The array elements in reverse order are:\n";for(autoit = arr.crbegin(); it != arr.crend(); it++)cout << *it <<" ";return0;}Output:The last element is 7 The array elements in reverse order are: 7 4 2 5 1
- array::crend() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the theoretical element right before the first element in the array container.
Syntax:
array_name.crend()
Parameters: The function does not accepts any parameter.
Return value: The function returns a constant reverse iterator pointing to the theoretical element right before the first element in the array container.
Program to demonstrate the array::crend() method:
Program 1:
// CPP program to illustrate// the array::crend() function#include <bits/stdc++.h>usingnamespacestd;intmain(){array<int, 5> arr = { 1, 5, 2, 4, 7 };// prints all the elementscout <<"The array elements in reverse order are:\n";for(autoit = arr.crbegin(); it != arr.crend(); it++)cout << *it <<" ";return0;}Output:The array elements in reverse order are: 7 4 2 5 1


