forward_list cbegin() in C++ STL

The forward_list::cbegin() is a function in C++ STL which returns a constant iterator pointing to the first element of the forward_list.

Syntax:

forward_list_name.cbegin()

Parameters: This function does not accept any parameter.



Return Value: This function returns an iterator that points to the const content. Since the iterator is not constant it can be increased or decreased or modified but as it cannot be used to modify its content even if the forward list is not constant. If the forward list is empty, the iterator returned by the function will not be dereferenced.

The following programs illustrate the use of the function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// forward_list::cbegin();
  
#include <forward_list>
#include <iostream>
using namespace std;
  
int main()
{
    forward_list<int> sample = { 45, 87, 6 };
  
    // Get the first element by
    // dereferencing the iterator
    // returned by sample.cbegin()
    cout << "1st element of sample: ";
    cout << *sample.cbegin();
}

chevron_right


Output:

1st element of sample: 45

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <forward_list>
#include <iostream>
using namespace std;
  
int main()
{
    forward_list<int> sample = { 7, 4, 9, 15 };
  
    // Display the elements
  
    cout << "sample: ";
    for (auto it = sample.cbegin(); it != sample.cend(); it++)
        cout << *it << " ";
}

chevron_right


Output:

sample: 7 4 9 15


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.