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

28 Sep, 2021
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.
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 Exceptions1. 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
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 Exceptions1. 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
 
chhabradhanvi
cpp-containers-library
CPP-forward-list
CPP-Library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects
Virtual Function in C++
Bitwise Operators in C/C++
Templates in C++ with Examples
Constructors in C++
Operator Overloading in C++
Socket Programming in C/C++
Object Oriented Programming in C++