
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
forward_list::front() and forward_list::empty() in C++ STL
In this article we will be discussing the working, syntax and examples of forward_list::front() and forward_list::empty() functions in C++.
What is a Forward_list in STL?
Forward list are sequence containers that allow constant time insert and erase operations anywhere within the sequence. Forward list are implement as a singly-linked lists. The ordering is kept by the association to each element of a link to the next element in the sequence.
What is forward_list::front()?
forward_list::front() is an inbuilt function in C++ STL which is declared in <forward_list> header file. front() returns the iterator which is referred to the first element in the forward_list container.
Syntax
forwardlist_container.front();
This function accepts no parameter.
Return Value
This function returns the iterator pointing to the first element of the container.
Example
/* In the below code we are creating a forward list and inserting elements to it then we will call front() function to fetch the first element in a forward list. */
#include <forward_list> #include <iostream> using namespace std; int main(){ forward_list<int> forwardList = {2, 6, 1, 0 }; cout<<"my first element in a forward list is: "; cout<<forwardList.front(); return 0; }
Output
If we run the above code it will generate the following output
my first element in a forward list is: 2
What is forward_list::empty()?
forward_list::empty() is an inbuilt function in C++ STL which is declared in <forward_list> header file. empty() returns true if the forward list container is empty, else it will return false. This function checks if the size of the container is 0
Syntax
bool forwardlist_container.empty();
This function accepts no parameter.
Return Value
This function returns true if the container’s size is 0 else it will return false
Example
/* In the below code we are creating a forward list then we will check whether the list is shown empty or not by calling the empty() function. After that, we will insert elements to a forward list and then we will again call empty() function to check what will be the result now. */
#include <forward_list> #include <iostream> using namespace std; int main(){ forward_list<int> forwardList = {}; if (forwardList.empty()){ cout << "Yess forward list is empty\n"; } forwardList = {1, 3, 4, 5}; if (forwardList.empty()){ cout << "Yess forward list is empty\n"; } else { cout << "No forward list is not empty\n"; } return 0; }
Output
If we run the above code it will generate the following output
Yess forward list is empty No forward list is not empty
- Related Articles
- forward list::cend() in C++ STL
- Forward list assign() function in C++ STL
- list::front() and list::back() in C++ STL
- list front() function in C++ STL
- Golang Program to create a doubly linked list and traverse forward.
- list empty() function in C++ STL
- Store – and – Forward Packet Switching
- Differentiate hedging and forward contract
- Program to check there is any forward path in circular cyclic list or not in Python
- list begin( ) and list end( ) in C++ STL
- list::emplace_front() and list::emplace_back() in C++ STL
- list::pop_front() and list::pop_back() in C++ STL
- list::push_front() and list::push_back() in C++ STL
- Difference between Forward and Future Contract
- Forward Error Correction (FEC)
