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

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 behaviour.
2. It has a no exception throw guarantee if the forward list is not empty.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

Output:

1
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

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 initialised 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

Output

26

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.




Article Tags :
C++
Practice Tags :