Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ front() function

C++ front() function

C++ Container Class The C++ container class


front is used in list toget the first element.

front is a member function of the container class, used to return the first element of the linked list.

The time complexity is O(1).

Word meaning: front means "front," i.e., to get the first element.


Basic syntax and parameters

Syntax format



reference front();

const_reference front() const;

Function description


Example

Example 1: Get the first element

Example

#include

#include

int

main

(

)

{

    std

::

list

<

int

numbers

=

{

10

,

20

,

30

,

40

,

50

}

;

    std

::

cout

<<

"First element: "

<<

numbers.

front

(

)

<<

std

::

endl

;

   

// Modify the first element

    numbers.

front

(

)

=

100

;

    std

::

cout

<<

"Modified first element: "

<<

numbers.

front

(

)

<<

std

::

endl

;

   

return

0

;

}

Expected output:

第一个元素: 10

修改后第一个元素: 100


C++ Container Class The C++ container class