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.
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.
// 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 Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// 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 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.
// 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
Recommended Posts:
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Left-Right traversal of all the levels of N-ary tree
- Difference between Iterators and Pointers in C/C++ with Examples
- ostream::seekp(pos) method in C++ with Exmaples
- Default Methods in C++ with Examples
- C++ Tutorial
- Hello World Program : First program while learning Programming
- Difference between Argument and Parameter in C/C++ with Examples
- <cfloat> float.h in C/C++ with Examples
- C/C++ #include directive with Examples
- C/C++ if else statement with Examples
- C/C++ if statement with Examples
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.