C++ Forward_list::begin() Function



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

If the current forward_list is empty, the iterator returned by the begin() 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::begin() function −

iterator begin();

Parameters

  • It does not accept any parameter.

Return value

This function returns an iterator to the first element.

Example 1

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

In the following example, we are using the C++ std::forward_list::begin() function to retrieve an iterator to the first element of the current forward_list {1, 2, 3, 4, 5}.

#include<iostream>
#include<forward_list>
using namespace std;
int main(){
   //create a forward_list
   forward_list<int> num_list = {1, 2, 3, 4, 5};
   cout<<"The num_list contents are: "<<endl;
   for(int n : num_list) {
      cout<<n<<endl;
   }
   //using the begin() function
   auto it = num_list.begin();
   cout<<"The iterator to the first element: "<<*it;
}

Output

Following is the output of the above program −

The num_list contents are: 
1
2
3
4
5
The iterator to the first element: 1

Example 2

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

Following is another example of the C++ d::forward_list::begin() function. Here, we are creating a forward_list(type char) named char_list with contents {'a', 'b', 'c', 'd', 'e'}. Then, using the begin() function, we are trying to retrieve an 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 begin() function
   auto it = char_list.begin();
   cout<<"The iterator to the first element: "<<*it;
}

Output

This will generate the following output −

The char_list contents are: 
a
b
c
d
e
The iterator to the first element: a

Example 3

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

In the following program, we are creating a forward_list(type string) named fruits with contents {"Apple", "Banana", "Orange", "Grapes"}. Then, using the for-loop along with the begin() function to loop through this forward_list and retrieve an 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> fruits = {"Apple", "Banana", "Orange", "Grapes"};
   cout<<"The iterator of all the elements are: "<<endl;
   for(auto it = fruits.begin(); it != fruits.end(); it++){
      cout<<*it<<endl;
   }
}

Output

The above program produces the following output −

The iterator of all the elements are: 
Apple
Banana
Orange
Grapes

Example 4

If the forward_list is empty, the returned 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 begin() function
   auto it = fruits.begin();
   cout<<*it<<endl;
}

Output

On executing the above program, it will produce the following output −

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