C++ List Library - front() Function


Advertisements

Description

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

Declaration

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

C++98

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::list::front() function.

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l = {1, 2, 3, 4, 5};

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

   return 0;
}

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

First element of the list = 1
list.htm
Advertisements