Skip to content
Related Articles

Related Articles

forward_list::front() and forward_list::empty() in C++ STL

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 10 Aug, 2022
Improve Article
Save Article

Forward list in STL implements singly linked list. Introduced from C++11, forward list are 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 forward list keeps track of location of only next element while list keeps track to both next and previous elements.

forward_list::front()

This function is used to reference the first element of the forward list container. This function can be used to fetch the first element of a forward list.

Syntax : 

forwardlistname.front()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the first element of the container.

Examples: 

Input  : forward_list forwardlist{1, 2, 3, 4, 5};
         forwardlist.front();
Output : 1

Input  : forward_list forwardlist{0, 1, 2, 3, 4, 5};
         forwardlist.front();
Output : 0

Errors and Exceptions
1. If the forward list container is empty, it causes undefined behavior. 
2. It has a no exception throw guarantee if the forward list is not empty.

C++




// CPP program to illustrate
// Implementation of front() function
#include <forward_list>
#include <iostream>
using namespace std;
 
int main()
{
    forward_list<int> myforwardlist{ 1, 2, 3, 4, 5 };
    cout << myforwardlist.front();
    return 0;
}

Output: 

1

Time Complexity: O(n)

forward_list::empty()

empty() function is used to check if the forward list container is empty or not.

Syntax :  

forwardlistname.empty()
Parameters :
No parameters are passed.
Returns :
True, if list is empty
False, Otherwise

Examples: 

Input  : forward_list forwardlist{1, 2, 3, 4, 5};
         forwardlist.empty();
Output : False

Input  : forward_list forwardlist{};
         forwardlist.empty();
Output : True

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

C++




// CPP program to illustrate
// Implementation of empty() function
#include <forward_list>
#include <iostream>
using namespace std;
 
int main()
{
    forward_list<int> myforwardlist{};
    if (myforwardlist.empty()) {
        cout << "True";
    }
    else {
        cout << "False";
    }
    return 0;
}

Output: 

True

Application – front() and empty() : Given a list of integers, find the sum of the all the integers. 

Input  : 1, 5, 6, 3, 9, 2
Output : 26
Explanation -  1+5+6+3+9+2 = 26

Algorithm : 
1. Check if the forward list is empty, if not add the front element to a variable initialized as 0, and pop the front element. 
2. Repeat this step until the forward list is empty. 
3. Print the final value of the variable.

C++




// CPP program to illustrate
// Application of empty() function
#include <forward_list>
#include <iostream>
using namespace std;
 
int main()
{
    int sum = 0;
    forward_list<int> myforwardlist{ 1, 5, 6, 3, 9, 2 };
    while (!myforwardlist.empty()) {
        sum = sum + myforwardlist.front();
        myforwardlist.pop_front();
    }
    cout << sum;
    return 0;
}

Output 

26

Let us see the differences in a tabular form -:

 forward_list::front()forward_list::empty()
1.It is used to return a reference to the first element in the forward_list container.It is used to check  whether the forward_list container is empty
2.Its syntax is -:
  reference front();

Its syntax is -:

empty();

3.It does not take any parameters.It does not take any parameters.
4.Its complexity is constant.Its complexity is constant.
5.Its iterator validity does not changes.Its iterator validity does not changes.

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!