Skip to content
Related Articles

Related Articles

forward_list cbegin() in C++ STL
  • Last Updated : 11 Oct, 2018

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:




// 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();
}


Output:

1st element of sample: 45

Program 2:




#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 << " ";
}


Output:

sample: 7 4 9 15

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 :