Skip to content
Related Articles

Related Articles

array::rbegin() and array::rend() in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 30 Oct, 2019
  1. 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
    
  2. 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
    
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 foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :