Skip to content
Related Articles

Related Articles

array::crbegin() and array::crend() in C++ STL
  • Last Updated : 17 Oct, 2019
  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()

    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 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
    
  2. 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>
    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
    
My Personal Notes arrow_drop_up
Recommended Articles
Page :