list rbegin() and rend() function in C++ STL

  1. list::rbegin() is an inbuilt function in C++ STL that returns a reverse iterator which points to the last element of the list.

    Syntax:

    list_name.rbegin()

    Parameter: This function does not accept any parameters.

    Return value: It returns a reverse iterator which points to the last element of the list.

    Below program illustrates the above function:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // list::rbegin() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        list<int> lis = { 10, 20, 30, 40, 50 };
      
        cout << "The list in reverse order: ";
      
        for (auto it = lis.rbegin(); it != lis.rend(); ++it)
            cout << *it << " ";
      
        return 0;
    }

    chevron_right

    
    

    Output:

    The list in reverse order: 50 40 30 20 10
    
  2. list::rend() is an inbuilt function in C++ STL that returns a reverse iterator which points to the position before the beginning of the list.

    Syntax:

    list_name.rend()

    Parameter: The function does not accept any parameters.

    Return value: It returns a reverse iterator which points to the position before the beginning of the list.

    Below program illustrates the above function:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // list::rbegin() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        list<int> lis = { 10, 20, 30, 40, 50 };
      
        cout << "The list in reverse order: ";
      
        for (auto it = lis.rbegin(); it != lis.rend(); ++it)
            cout << *it << " ";
      
        return 0;
    }

    chevron_right

    
    

    Output:

    The list in reverse order: 50 40 30 20 10
    


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.