array::cbegin() and array::cend() in C++ STL

  1. array::cbegin() is a built-in function in C++ STL which returns a const_iterator pointing to the first element in the array. It cannot be used to modify the element in the array which is possible using array::begin().

    Syntax:

    array_name.cbegin() 

    Parameters: The function does not accept any parameters.

    Return Value: The function returns a const_iterator pointing to the first element in the array.

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to illustrate
    // the array::cbegin() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        array<int, 5> arr = { 1, 5, 2, 4, 7 };
      
        // Prints the first element
        cout << "The first element is " << *(arr.cbegin()) << "\n";
      
        // Print all the elements
        cout << "The array elements are: ";
        for (auto it = arr.cbegin(); it != arr.cend(); it++)
            cout << *it << " ";
        return 0;
    }

    chevron_right

    
    

    Output:



    The first element is 1
    The array elements are: 1 5 2 4 7
    
  2. array::cend() is a built-in function in C++ STL which returns a const_iterator pointing to the theoretical element after the last element in an array.

    Syntax:

    array_name.cend() 

    Parameters: The function does not accept any parameters.

    Return Value: The function returns a const_iterator pointing to the theoretical element after the last element in an array.

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to illustrate
    // the array::cend() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        array<int, 5> arr = { 1, 5, 2, 4, 7 };
      
        // prints all the elements
        cout << "The array elements are: ";
        for (auto it = arr.cbegin(); it != arr.cend(); it++)
            cout << *it << " ";
        return 0;
    }

    chevron_right

    
    

    Output:

    The array elements are: 1 5 2 4 7
    

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.




My Personal Notes arrow_drop_up

Recommended Posts:

    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.