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
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.
#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:
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.
#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:
front() is : j