Open In App

array::crbegin() and array::crend() in C++ STL

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report
  1. 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()
  1. Parameters: The function does not accepts any parameter. 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




// CPP program to illustrate
// the array::crbegin() 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.crbegin()) << endl;
 
    // prints all the elements
    cout << "The array elements in reverse order are:\n";
    for (auto it = arr.crbegin(); it != arr.crend(); it++)
        cout << *it << " ";
 
    return 0;
}


Output:

The last element is 7
The array elements in reverse order are:
7 4 2 5 1
  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()
  1. 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




// CPP program to illustrate
// the array::crend() 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.crbegin(); it != arr.crend(); it++)
        cout << *it << " ";
    return 0;
}


Output:

The array elements in reverse order are:
7 4 2 5 1

Let us see the differences in a tabular form -:

  array::crbegin() array::crend()
1. It is used to return a const_reverse_iterator pointing to the last element in the array container. It is used to return  a const_reverse_iterator pointing to the theoretical element preceding the first element in the vector
2.

Its syntax is -:

const_reverse_iterator crbegin();

Its syntax is -:

const_reverse_iterator crend();

3. It does not take any parameters. It does not take any parameters.
4. Its complexity is constant. Its complexity is constant.
5. Its iterator validity does not changes. Its iterator validity does not changes.


Last Updated : 10 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads