Skip to content
Related Articles

Related Articles

forward_list::begin() and forward_list::end() in C++ STL

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Jun, 2022
View Discussion
Improve Article
Save Article

Forward list in STL implements singly linked list. Introduced from C++11, the forward list is more useful than other containers in insertion, removal, and moving operations (like sort) and allows time constant insertion and removal of elements. It differs from list by the fact that the forward list keeps track of the location of only the next element while the list keeps track of both the next and previous elements.

forward_list::begin()

begin() function is used to return an iterator pointing to the first element of the forward list container. begin() function returns a bidirectional iterator to the first element of the container. 

Syntax:

forwardlistname.begin()

Parameters: NA

Return Type: This function returns a bidirectional iterator pointing to the first element.

Examples:

Input  : myflist{1, 2, 3, 4, 5};
         myflist.begin();
Output : returns an iterator to the element 1

Input  : myflist{8, 7};
         myflist.begin();
Output : returns an iterator to the element 8

Errors and Exceptions 1. It has a no exception throw guarantee. 2. Shows error when a parameter is passed. 

CPP




// CPP program to illustrate
// Implementation of begin() function
#include <forward_list>
#include <iostream>
using namespace std;
 
int main()
{
    // declaration of forward list container
    forward_list<int> myflist{ 1, 2, 3, 4, 5 };
 
    // using begin() to print list
    for (auto it = myflist.begin(); it != myflist.end(); ++it)
        cout << ' ' << *it;
    return 0;
}


Output:

1 2 3 4 5

Time Complexity: O(1)

forward_list::end()

end() function is used to return an iterator pointing to the last element of the list container. end() function returns a bidirectional iterator to the last element of the container. 

Syntax:

forwardlistname.end()

Parameters: NA

Return Type: This function returns a bidirectional iterator pointing to the last element.

Examples:

Input  : myflist{1, 2, 3, 4, 5};
         myflist.end();
Output : returns an iterator to the element 5

Input  : myflist{8, 7};
         myflist.end();
Output : returns an iterator to the element 7

Errors and Exceptions 1. It has a no exception throw guarantee. 2. Shows error when a parameter is passed. 

CPP




// CPP program to illustrate
// Implementation of end() function
 
#include& lt; forward_list & gt;
#include& lt; iostream & gt;
using namespace std;
 
int main()
{
    // declaration of forward list container
    forward_list& lt;
    int& gt;
    myflist{ 1, 2, 3, 4, 5 };
 
    // using end() to print forward list
    for (auto it = myflist.begin(); it != myflist.end();
         ++it)
        cout& lt;
    <
    ' ' & lt;
    <
    *it;
    return 0;
}


Output:

1 2 3 4 5

Time Complexity: O(1)

Let us see the differences in a tabular form as follows: 

forward_list::begin()   forward_list::end()
It is used to return an iterator pointing to the first element in the forward_list container. It is used to return an iterator referring to the past-the-end element in the forward_list container.

Its syntax is as follows:

iterator begin()

Its syntax is as follows:

iterator end()
It does not take any parameters It does not take any parameters.
Its complexity is constant. Its complexity is constant.
Its iterator validity does not change. Its iterator validity does not change.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!