std::forward_list<T,Allocator>::front - cppreference.com

From cppreference.com

referencefront(); (1) (since C++11)const_referencefront()const; (2) (since C++11)Returns a reference to the first element in the container.

If

empty()

is true, the behavior is undefined.

(until C++26)If

empty()

is true:

If the implementation is

hardened

, a

contract violation

occurs.

If the implementation is not hardened, the behavior is undefined.

(since C++26)Return value

Reference to the first element.

Complexity

Constant.

Notes

For a container c, the expression c.front() is equivalent to *c.begin().

Example

The following code uses front to access the first element of a std::forward_list<char>:

Run this code

#include<cassert>#include<forward_list>intmain(){std::forward_list<char>letters{'a','b','c','d'};assert(letters.front()=='a');}See also