list crbegin() and crend() function in C++ STL

  1. The list::crbegin() is a built-in function in c++ STL that returns a constant reverse iterator which points to the last element of the list i.e reversed beginning of container. The elements cannot be modified or changed as the iterator is a constant one.

    Syntax:

    list_name.crbegin()

    Parameters: The function does not accept any parameters.

    Return value: It returns a random access reverse iterator which points to the reverse 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::crbegin() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // declaration of the list
        list<int> lis = { 10, 20, 30, 40, 50 };
      
        // prints the last element
        cout << "The last element is: " << *lis.crbegin();
        cout << "\nList: ";
      
        for (auto it = lis.crbegin(); it != lis.crend(); ++it)
            cout << *it << " ";
      
        return 0;
    }

    chevron_right

    
    

    Output:

    The last element is: 50
    List: 50 40 30 20 10
    
  2. The list::crend() is a built-in function in C++ STL that returns a constant reverse iterator which points to the theoretical element preceding the first element in the list i.e. the reverse end of the list. The elements cannot be modified or changed as the iterator is a constant one.

    Syntax:

    list_name.crend()
    

    Parameters: The function does not accept any parameters.

    Return value: It returns a constant random reverse iterator which points to the reverse end 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::crend() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // declaration of the list
        list<int> lis = { 7, 6, 5, 4, 3, 2 };
      
        cout << "List: " << endl;
      
        for (auto it = lis.crbegin(); it != lis.crend(); ++it)
            cout << *it << " ";
      
        return 0;
    }

    chevron_right

    
    

    Output:

    List: 
    2 3 4 5 6 7
    


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 :


Be the First to upvote.


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