C++ Forward_list::cbefore_begin() Function



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

The element of this forward_list acts as a placeholder, attempting to access it results in undefined behavior. The returned iterator can be used with the emplace_after(), erase_after(), insert_after(), and splice_after() functions to insert, erase, and delete an element at the returned iterator position.

The cbefore_begin() function is similar to the before_begin() function in C++ std:: forward_list.

Syntax

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

const_iterator cbefore_begin();

Parameters

  • It does not accept any parameter.

Return value

This function returns a constant iterator to the element before the first element.

Example 1

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

In the following program, we are using the C++ std::forward_list::cbefore_begin() function to retrieve a constant iterator to the element before the first element of this 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 cbefore_begin() function
   auto it = num_list.cbefore_begin();
   cout<<"The constant iterator to the element 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: 
1
2
3
4
5
The constant iterator to the element before the first element is: 0

Example 2

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

Following is another example of the C++ std::forward_list::cbefore_begin() function. Here, we are creating a forward_list(type char) named char_list with contents {'B', 'C', 'E', 'F'}. Then, we use the cbefore_begin() function to retrieve a constant iterator to the element before the first element of this container, 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 cbefore_begin() function
   auto it = char_list.cbefore_begin();
   cout<<"The constant 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
The constant iterator is: �
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 a constant iterator of the string-type forward_list element.

In this example, we are creating a forward_list(type string) named fruits with contents {"Apple", "Banana", "Orange", "Grapes", "Guava"}. Then, using the cbefore_begin() function, we are trying to retrieve a constant iterator to the element before the first element of this container, and using the insert_after() function, we are trying to insert a new element "Pear" at the returned iterator position.

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<string> fruits = {"Apple", "Banana", "Orange", "Grapes", "Guava"};
   cout<<"The fruits forward_list contents are: "<<endl;
   for(string n : fruits){
      cout<<n<<endl;
   }
   //using the cbefore_begin() function
   auto it = fruits.cbefore_begin();
   cout<<"The constant iterator is: "<<*it<<endl;
   //using the insert_after()function
   fruits.insert_after(it, "Pear");
   cout<<"The fruits forward_list contents after inserting new element: "<<endl;
   for(string s : fruits){
      cout<<s<<endl;
   }
}

Output

Following is the output of the above program −

The fruits forward_list contents are: 
Apple
Banana
Orange
Grapes
Guava
The constant iterator is: 
The fruits forward_list contents after inserting new element: 
Pear
Apple
Banana
Orange
Grapes
Guava
Advertisements