Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ forward_list::front function is used to return a reference to the first element of the forward_list. Please note that, Unlike the forward_list::begin function, which returns the iterator pointing to the first element, it returns the a direct reference to the same element of the forward_list.

Syntax

reference front();
const_reference front() const;

Parameters

No parameter is required.

Return Value

A reference to the first element of the forward_list.

Time Complexity

Constant i.e, Θ(1).

Example:

In the below example, the forward_list::front function is used to access the first element of the forward_list f_list.

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> f_list{10, 20, 30, 40, 50};

  cout<<"The first element of f_list is: ";
  cout<<f_list.front();

  cout<<"\n\nAdd 100 to the first element of the f_list.\n\n";
  f_list.front() = f_list.front() + 100;

  cout<<"Now, The first element of f_list is: ";
  cout<<f_list.front();  
  return 0;
}

The output of the above code will be:

The first element of f_list is: 10

Add 100 to the first element of the f_list.

Now, The first element of f_list is: 110

❮ C++ - Forward List