Open In App

forward_list::before_begin() in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

forward_list::before_begin() is an inbuilt function in C++ STL that returns an iterator that points to the position before the first element of the forward_list. Forward list in STL is a singly linked list implementation. This function comes under the <forward_list> header file.

Syntax: 

forwardlist_name.before_begin()

Return value: The function returns an iterator that points to the position before the first element of the forward_list.
The below program demonstrates the above function: 

CPP




// C++ program to illustrate the
// before_begin() function
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
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 

Time Complexity: O(1)

Auxiliary Space: O(1)

Must Read: 


Last Updated : 21 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads