List Front Function - C++ Programming Tutorial
C++ Course / Dynamic Programming / List Front Function

List Front Function

BLUF: Mastering List Front Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: List Front Function

C++ is renowned for its efficiency. Learn how List Front Function enables low-level control and high-performance computing in the tutorial below.

C++ List front

The front method in C++ List retrieves the initial element of the list, offering direct access to that specific element.

Difference between front and begin

The begin function provides an iterator pointing to the element, whereas the front function gives a direct reference to that particular element.

Syntax

Example

reference front();

Parameter

It does not contain any parameter.

Return value

It provides the explicit pointer to the initial element.

Example 1

Let's examine a basic scenario where the list consists of integer values.

Example

#include <iostream>
#include<list>
using namespace std;
int main()
{
    std::list<int> li={1,2,3,4,5};
    std::cout <<"front() is :"<< li.front() << std::endl;
    return 0;
}

Output:

Output

front() is : 1

In this instance, the front method retrieves the initial element from the list, which is 1.

Example 2

Let's examine a basic illustration involving a list that holds character values.

Example

#include <iostream>
#include<list>
using namespace std;
int main()
{
    std::list<char> li={'j','a','v','a'};
    std::cout <<"front() is :"<< li.front() << std::endl;
    return 0;
}

Output:

Output

front() is : j

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience