Skip to content
Related Articles

Related Articles

Improve Article

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:




    // 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;
    }
    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:




    // 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;
    }
    Output:
    List: 
    2 3 4 5 6 7
    
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 :