C++ Forward_list::before_begin() Function



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

The element of this forward_list acts as a placeholder. The iterator returned by this function can be used with emplace_after(), erase_after(), insert_after(), and splice_after() functions to insert, erase, and delete an element at the returned iterators’ position. The before_begin() function is similar to the cbefore_begin() function in C++ std:: forward_list.

Syntax

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

iterator before_begin();

Parameters

  • It does not accept any parameter.

Return value

This function returns an iterator to the element before the first element.

Example 1

If the forward_list is an int-type, the before_begin() function returns an iterator to the element before the first element.

In the following program, we are using the C++ std::forward_list::before_begin() function to retrieve an iterator to the element before the first element of this 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 before_begin() function
   auto it = num_list.before_begin();
   cout<<"An iterator which points to the position before the first element is: "<<*it;
}

Output

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

The num_list contents are:
10
20
30
40
50
An iterator which points to the position before the first element is: 0

Example 2

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

Following is another example of the C++ std::forward_list::before_begin() function. Here, we are creating a forward_list(type char) named char_list with contents {'b', 'c', 'd', 'e'}. Then, we use the before_begin() function to retrieve an iterator to the element before the first element of this forward_list, and using the emplace_after() function, insert a new element 'a' at the returned iterator position.

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<char> char_list = {'b', 'c', 'd', 'e'};
   cout<<"The char_list contents are: "<<endl;
   for(char c : char_list){
      cout<<c<<endl;
   }
   //using the before_begin() function
   auto it = char_list.before_begin();
   cout<<"An iterator is: "<<*it<<endl;
   //using emplace_after() function
   char_list.emplace_after(it, 'a');
   cout<<"The char_list contents after inserting new element: "<<endl;
   for(char c: char_list){
      cout<<c<<endl;
   }
}

Output

This will generate the following output −

The char_list contents are: 
b
c
d
e
An iterator is: 0
The char_list contents after inserting new element: 
a
b
c
d
e

Example 3

Apart from the int-type and char-type forward_list, we can also retrieve an iterator of the string-type forward_list element.

In this example, we are creating a forward_list(type string) named names with contents {"Raju", "Aman", "Rahul", "Ganesh"}. Then, using the before_begin() function, we are trying to retrieve an iterator to the element before the first element, and using the insert_after() function, we are trying to insert a new element "Ramesh" at the returned iterator position.

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<string> names = {"Raju", "Aman", "Rahul", "Ganesh"};
   cout<<"The names forward_list contents are: "<<endl;
   for(string n : names){
      cout<<n<<endl;
   }
   //using the before_begin() function
   auto it = names.before_begin();
   cout<<"An iterator is: "<<*it<<endl;
   //using the insert_after()function
   names.insert_after(it, "Ramesh");
   cout<<"The names forward_list contents after inserting new element: "<<endl;
   for(string s : names){
      cout<<s<<endl;
   }
}

Output

Following is the output of the above program −

The names forward_list contents are: 
Raju
Aman
Rahul
Ganesh
An iterator is: 
The names forward_list contents after inserting new element: 
Ramesh
Raju
Aman
Rahul
Ganesh
Advertisements