Skip to content
Related Articles

Related Articles

list crbegin() and crend() function in C++ STL
  • Last Updated : 19 Jul, 2018
  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
    

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :