C++ List::front() Function



The C++ std::list::front() function is used to retrieve the reference to the first element in the container.

In C++, a reference is a value that enables a program to indirectly access a particular data, such as a variable or record. If the current container(or list) is non-empty, the expression list.front() is equivalent to *list.begin(). The front() function returns zero if the current list(type int) is empty.

Calling the front() function on an empty list causes undefined behaviour.

Syntax

Following is the syntax of the C++ std::list::front() function −

reference front();   

Parameters

  • It does not accepts any parameter.

Return Value

This function returns the reference to the first element in the list.

Example 1

In the following program, we are using the std::list::front() function to retrieve the first element of the current list {10, 20, 30, 40, 50}.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create an integer list
   list<int> l = {10, 20, 30, 40, 50};
   cout<<"The list elements are:"<<endl;
   for(int lst : l){
      cout<<lst<<" ";
   }
   cout<<"\nThe first element of the list is: "<<l.front()<< endl;
   return 0;
}

Output

On executing the above program, it will produce the following output −

The list elements are: 10 20 30 40 50 
The first element of the list is: 10

Example 2

Following is another example of the std::list::front() function. Here, we are creating a list (type char) named char_list with the values {'+','-','@','#','$'}. Then, using the front() function, we are trying to retrieve the first element of the current list {'+','-','@','#','$'}.

#include <iostream>
#include <list>
using namespace std;

int main() {
   //create a character list
   list<char>char_list = {'+','-','@','#','$'};
   cout<<"The list elements are: ";
   for(char lst : char_list){
      cout<<lst<<" ";
   }
   cout<<"\nThe first element of the list is: "<<char_list.front()<< endl;
   return 0;
}

Output

Following is the output of the above program −

The list elements are: + - @ # $ 
The first element of the list is: +

Example 3

If the current list is a string type.

The following program display the first element of the current list(type string) {'Mango', 'Orange', 'Banana', 'Apple'}.

#include <iostream>
#include <list>
#include<string>
using namespace std;

int main() {
   list<string> Name;
   Name.push_back("Mango");
   Name.push_back("Orange");
   Name.push_back("Banana");
   Name.push_back("Apple");
   cout<<"The list elements are: ";
   for(string s: Name){
      cout<<s<<" ";
   }
   string name = Name.front();
   cout <<"\nThe first element in list is: "<<name;
   return 0;
}

Output

The above program generates the output as follows:

The list elements are: Mango Orange Banana Apple 
The first element in list is: Mango

Example 4

If the list(type int) is empty, this function returns zero(0).

In this program, we are creating an empty list(type int). Then, using the std::list::front() function, we are trying to get the first element of the current list(empty).

#include <iostream>
#include <list>
using namespace std;

int main() {
   //create a character list
   list<int> l = {};
   cout<<"The list elements are: ";
   for(int lst : l){
      cout<<lst<<" ";
   }
   cout<<"\nThe first element of the list is: "<<l.front()<<endl;
   return 0;
}

Output

The above program generates the following output −

The list elements are: 
The first element of the list is: 0

Example 5

In the following example, we create a list(type char) with the values {'a', 'b','c', 'd'}. Then, we declare an int variable named result that stores the value of list.front() function, and when we print the result variable value, it displays the ASCII value of the first element of this list.

#include <iostream>
#include <list>
using namespace std;

int main() {
   list<char> char_list = {'a', 'b','c', 'd'};
   cout<<"The list elements are: ";
   for(char lst: char_list){
      cout<<lst<<" ";
   }
   //declare an int variable
   int result;
   result = char_list.front();
   cout<<"\nThe first element of the list is: "<<char_list.front()<<endl;
   cout<<"The ASCII value of "<<result<<" is: "<<result;
 }

Output

After executing the above program, it produces the following output −

The list elements are: a b c d 
The fast element of the list is: a
The ASCII value of 97 is: 97
Advertisements