C++ Forward_list Library - front() Function


Advertisements

Description

The C++ function std::forward_list::front() returns a reference to the first element of the forward_list.

Declaration

Following is the declaration for std::forward_list::front() function form std::forward_list header.

C++11

reference front();
const_reference front() const;

Parameters

None

Return value

Returns a constant reference if object is constant qualified otherwise non-constant reference.

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::forward_list::front() function.

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl = {1, 2, 3, 4, 5};

   cout << "First element of the list = " << fl.front() << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

First element of the list = 1
forward_list.htm
Advertisements