forward_list::cbefore_begin() in C++ STL

forward_list::cbefore_begin() is an inbuilt function in CPP STL which returns a constant random access iterator which points to the position before the first element of the forward_list. The iterator obtained by this function can be used to iterate in the container but cannot be used to modify the content of the object to which it is pointing, even if the object itself is not constant.

Syntax:

forwardlist_name.cbefore_begin()

Parameters: The function does not accept any parameter.

Return value: The function returns a constant random access iterator which points to the position before the first element of the forward_list.

Below program demonstrates the above function:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// cbefore_begin() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialising the forward list
    forward_list<int> fl = { 20, 30, 40, 50 };
  
    // performing cbefore_begin function
    auto it = fl.cbefore_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;
}

chevron_right


Output:

Element of the list are:
10 20 30 40 50


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.