Skip to content
Related Articles

Related Articles

Improve Article

forward_list::before_begin() in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 27 Nov, 2018

forward_list::before_begin() is an inbuilt function in CPP STL which returns a iterator which points to the position before the first element of the forward_list.

Syntax:

forwardlist_name.before_begin()

Parameters: The function does not accept any parameter.

Return value: The function returns an iterator which points to the position before the first element of the forward_list.

Below program demonstrates the above function:




// C++ program to illustrate the
// before_begin() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialising the forward list
    forward_list<int> fl = { 20, 30, 40, 50 };
  
    // performing before_begin function
    auto it = fl.before_begin();
  
    // inserting element before the first element
    fl.insert_after(it, 10);
  
    cout << "Element of the list are:" << endl;
  
    // loop to print the elements of the list
    for (auto it = fl.begin(); it != fl.end(); ++it)
        cout << *it << " ";
  
    return 0;
}
Output:
Element of the list are:
10 20 30 40 50
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :