forward_list::cbefore_begin() in C++ STL

C++Server Side ProgrammingProgramming

Given is the task to show the working of forward_list::cbefore_begin() function in C++.

A forward_list only keeps linkage with the next element unlike normal list that keeps linkage with the next as well as the preceding elements, which helps iterations in both directions. But forward_list can only iterate in the forward direction.

The forward_list::cbefore_begin() function is a part of the C++ standard template library. It is used to obtain the position before the first element of the list.

<forward_list> header file should be included to call this function.

Syntax

Forward_List_Name.cbefore_begin();

Parameters

The function does not accept any parameter.

Return Value

The function returns a constant iterator that points at the position before the first element of the forward_list.

Example

Input: 8, 9, 32, 21
Output: 56 8 9 32 21

Explanation  − Here we created a forward list with elements 8,9,32,21. Then we called the cbefore_begin() function that points at position before the first element of the list and stores that position in the iterator itr. Then we use the insert_after() function to insert the element 56 at the position before the first element, that is 8. So when we print it, the output generated is 56 8 9 32 21, which shows that 56 gets at the beginning of the list and hence the cbefore_begin function() worked correctly.

Approach used in the below program as follows 

  • First create a forward_list, let us say “Lt” of type int and assign it some values.
  • Then create an object “itr” of type auto and store in it, the iterator returned by calling the cbefore_begin() function.
  • Then use the insert_after() function to insert a new element in the beginning of the list. Pass the iterator “itr” as the first argument and the number to be inserted as the second.
  • Then start a For loop for printing the list
  • Then create an object “itr” of type auto inside the for loop for receiving the return values of end() and begin() function. Initialize “itr” by giving it the first element of the list using the begin() function.
  • Then specify the terminating condition of the for loop by writing “itr” not equal to the last element of the list using the cend() function.
  • Print *itr.

Algorithm

Start
Step 1->In function main()
   Initialize forward_list<int> Lt={}
   Initialize auto itr= Lt.cbefore_begin()
   Call insert_after(itr, new_element)
   Loop For auto itr = Ltcbegin() and itr != Lt.end() and itr++
   Print *itr
   End
Stop

Example

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   forward_list<int> Lt = { 40, 55, 67, 89 };
   auto itr = Lt.cbefore_begin();
   Lt.insert_after(itr, 77);
   for (auto itr = Lt.begin(); it != Lt.end(); ++itr)
   cout << *itr << " ";
   return 0;
}

Output

If we run the above code it will generate the following output −

77 40 55 67 89
raja
Published on 20-Jan-2020 14:36:22
Advertisements