C++ Forward_list::cbegin() Function



The C++ std::forward_list::cbegin() function is used to retrieve a constant iterator to the first element of the forward_list container.

If the current forward_list is empty, the constant iterator returned by the cbegin() function is equal to the result of the end() function. In C++, an iterator is an object that can iterate over the elements in an STL(standard template library) and provide access to an individual element.

Syntax

Following is the syntax of the C++ std::forward_list::cbegin() function −

const_iterator cbegin() const;

Parameters

  • It does not accept any parameter.

Return value

This function returns a constant iterator to the first element.

Example 1

If the forward_list is non-empty and an int-type, the cbegin() function returns a constant iterator to the first element.

In the following example, we are using the C++ std::forward_list::cbegin() function to retrieve a constant iterator to the first element of the current forward_list {10, 20, 30, 40, 50}.

#include<iostream>
#include<forward_list>
using namespace std;
int main(){
   //create a forward_list
   forward_list<int> num_list = {10, 20, 30, 40, 50};
   cout<<"The num_list contents are: "<<endl;
   for(int n : num_list) {
      cout<<n<<endl;
   }
   //using the cbegin() function
   auto it = num_list.cbegin();
   cout<<"The constant iterator to the first element: "<<*it;
}

Output

Following is the output of the above program −

The num_list contents are: 
10
20
30
40
50
The constant iterator to the first element: 10

Example 2

If the forward_list is char-type, this function returns a constant iterator to the first char element of this forward_list.

Following is another example of the C++ d::forward_list::cbegin() function. Here, we are creating a forward_list(type char) named char_list with contents {'A', 'B', 'C', 'D', 'E'}. Then, using the cbegin() function, we are trying to retrieve a constant iterator to the first element of this forward_list.

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<char> char_list = {'A', 'B', 'C', 'D', 'E'};
   cout<<"The char_list contents are: "<<endl;
   for(char c : char_list) {
      cout<<c<<endl;
   }
   //using the cbegin() function
   auto it = char_list.cbegin();
   cout<<"The constant iterator to the first element: "<<*it;
}

Output

This will generate the following output −

The char_list contents are: 
A
B
C
D
E
The constant iterator to the first element: A

Example 3

Using the for-loop along with the cbegin() function to loop through the container and retrieve a constant iterator of all the elements starting from the first element.

In the following program, we are creating a forward_list(type string) named colors with contents {"Red", "Green", "Blue", "Black"}. Then, using the for-loop along with the cbegin() function to loop through this forward_list and retrieve the constant iterator of all the elements starting from the first element.

#include<iostream>
#include<forward_list>
using namespace std;
int main(){
   //create a forward_list
   forward_list<string> colors = {"Red", "Green", "Blue", "Black"};
   cout<<"The iterator of all the elements are: "<<endl;
   for(auto it = colors.cbegin(); it != colors.end(); it++){
      cout<<*it<<endl;
   }
}

Output

The above program produces the following output −

The iterator of all the elements are: 
Red
Green
Blue
Black

Example 4

If the forward_list is empty, the returned constant iterator is equal to the end() function result.

#include<iostream>
#include<forward_list>
using namespace std;
int main(){
   //create a forward_list
   forward_list<string> fruits = {};
   cout<<"The size of this forward_list is: "<<endl;
   cout<<distance(fruits.begin(), fruits.end())<<endl;
   cout<<"The iterator of all the elements are: "<<endl;
   //using the cbegin() function
   auto it = fruits.cbegin();
   cout<<*it<<endl;
}

Output

Let us compile and run the above program, this will produce the following result −

The size of this forward_list is: 
0
The iterator of all the elements are: 
Segmentation fault
Advertisements