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
is true, the behavior is undefined.
(until C++26)If
is true:
If the implementation is
, a
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