list cbegin() and cend() function in C++ STL

  1. The list::cbegin() is a built-in function in C++ STL which returns a constant random access iterator which points to the beginning of the list. Hence the iterator obtained can be used to iterate container but cannot be used to modify the content of the object to which it is pointing even if the object itself is not constant.

    Syntax:

    list_name.cbegin()

    Parameters: The function does not accept any parameters.

    Return value: It returns a constant random access iterator which points to the beginning of the list.

    Below programs illustrate the above function:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // cbegin() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // declaration of list
        list<int> lis = { 5, 6, 7, 8, 9 };
      
        // Prints the first element
        cout << "The first element is: " << *lis.cbegin();
      
        // printing list elements
        cout << "\nList: ";
      
        for (auto it = lis.cbegin(); it != lis.end(); ++it)
            cout << *it << " ";
      
        return 0;
    }
    chevron_right
    
    
    Output:

    The first element is: 5
    List: 5 6 7 8 9
    
  2. The list::cend() is a built-in function in C++ STL which returns a constant random access iterator which points to the end of the list. Hence the iterator obtained can be used to iterate container but cannot be used to modify the content of the object to which it is pointing even if the object itself is not constant.

    Syntax:

    list_name.cend()

    Parameters: The function does not acceps any parameter.

    Return value: It returns a constant random access iterator which points to the end of the list.

    Below program illustrates the function:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // cend() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // declaration of list
        list<int> lis = { 10, 20, 30, 40, 50 };
      
        // printing list elements
        cout << "List: " << endl;
      
        for (auto it = lis.cbegin(); it != lis.cend(); ++it)
            cout << *it << " ";
      
        return 0;
    }
    chevron_right
    
    
    Output:
    List: 
    10 20 30 40 50
    

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.





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 :
C++
Practice Tags :