To faithfully explain the concept of forwardlist::cbeforebegin in C++ STL. We first discussed the List in C++n programming language's code and output below. The forward list c before begin function is an in-built functionality in STL [Standard Template Library (STL)]. It returns a constant random-access iterator which points to the position before the first element of the forward_list.
List in C++ STL
C++ code
Example
// CPP program to show the implementation of List
// Here we are writing down the C++ programming language code to
// demonstrate the concept of List in C++ programming language code
// in the Standard Template Library (STL)
#include <iostream>
#include <iterator>
#include <list>
using namespace std;
// function for printing the elements in a list
// the below few lines of code help us with displaying the elements that
// we have inserted into it which is a part of the C++ Standard Template
// Library (STL)
void showlist(list<int> g)
{
list<int>::iterator it;
for (it = g.begin(); it != g.end(); ++it)
cout << '\t' << *it;
cout << '\n';
}
// the main driver code functionality starts from here
int main()
{
// then below code helps us with creating a list in C++ STL dynamically
list<int> gqlist1, gqlist2;
// loop to push elements into the list which we have just created
for (int i = 0; i < 10; ++i) {
gqlist1.push_back(i * 2);
gqlist2.push_front(i * 3);
}
// cout statement helping us to print gqlist1 elements on console
cout << "\nThe List 1 has elements : ";
showlist(gqlist1);
// cout statement helping us to print gqlist2 elements on console
cout << "\nThe List 2 has elements : ";
showlist(gqlist2);
// cout statement helping us to print gqlist1 front elements on console
cout << "\n list1.front() : " << gqlist1.front();
// cout statement helping us to print gqlist1 back elements on console
cout << "\n list1.back() : " << gqlist1.back();
//cout statement helping us to print gqlist after poping elements on console
cout << "\n list1.pop_front() : ";
gqlist1.pop_front();
showlist(gqlist1);
//cout statement helping us to print list after poping elements on console
cout << "\n list2.pop_back() : ";
gqlist2.pop_back();
showlist(gqlist2);
//cout statement helping us to print gqlist after reverse elements on
// console
cout << "\n list1.reverse() : ";
gqlist1.reverse();
showlist(gqlist1);
//cout statement helping us to print gqlist after sort elements on console
cout << "\n list2.sort(): ";
gqlist2.sort();
showlist(gqlist2);
return 0;
// end of the C++ programming language code explaining
// forward_list::cbefore_begin() in C++ STL
}
Output:
Output
The List 1 has elements : 0 2 4 6 8 10 12 14 16 18
List 2 has elements : 27 24 21 18 15 12 9 6 3 0
list1.front() : 0
list1.back() : 18
list1.pop_front() : 2 4 6 8 10 12 14 16 18
list2.pop_back() : 27 24 21 18 15 12 9 6 3
list1.reverse() : 18 16 14 12 10 8 6 4 2
list2.sort(): 3 6 9 12 15 18 21 24 27
Syntax of forward_list::cbefore_begin
Example
forwardlist_name.cbefore_begin()
C++ code
Example
// Here we are writing down the c++ programming language code to
// demonstrate the concept of forward_list::cbefore_begin() in C++ STL
// where it illustrates the function cbefore_begin() in C++ programming code
#include <bits/stdc++.h>
using namespace std;
// the main driver code functionality starts from here
int main()
{
// the below code snippet helps us with creating a list with few
// initial values like 20, 30, 40 and ends with 50
forward_list<int> fl = { 20, 30, 40, 50 };
// here, we are trying to perform the function
// cbefore_begin helping us to use the stl library in the C++ language
auto it = fl.cbefore_begin();
// the below code snippet helps us with the insertion operation
// where the element will be added to the first place
fl.insert_after(it, 10);
cout << "the elements which have gotten inserted are as follows" << endl;
// the below for loop helps us with printing down the
// elements, which we have created in the list on our output screens
for (auto it = fl.begin(); it != fl.end(); ++it)
cout << *it << " ";
return 0;
// end of the C++ programming language code explaining
// forward_list::cbefore_begin() in C++ STL
}
Output:
Output
the elements which have gotten inserted are as follows
10 20 30 40 50