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


Among the various operations on vector,front is a very simple function, used toget the first element.

front is a member function of the container class, used toreturns the first element of the container. It is equivalent to at(0) or operator[](0), but with clearer semantics.

front provides an intuitive way to access the container's first element.

Word meaning: front means 'front', i.e., to get the first (frontmost) element.


Basic syntax and parameters

front is a member function of the container class; calling it is straightforward and requires no arguments.

Syntax format



reference front();

const_reference front() const;

Parameter Description

- `front` takes no arguments.

Function description


Example

Let us thoroughly master front through a series of examples from simple to complex.

Example 1: Basic usage - getting the first element

Example

#include

#include

int

main

(

)

{

   

// 1. Create a vector and add some elements

    std

::

vector

<

int

numbers

=

{

10

,

20

,

30

,

40

,

50

}

;

    std

::

cout

<<

"vector size is: "

<<

numbers.

size

(

)

<<

std

::

endl

;

   

// 2. Use front to get the first element

    std

::

cout

<<

"First element (front): "

<<

numbers.

front

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Using at(0): "

<<

numbers.

at

(

0

)

<<

std

::

endl

;

    std

::

cout

<<

"Using [0]: "

<<

numbers

[

0

]

<<

std

::

endl

;

   

return

0

;

}

Expected output:

vector的大小是: 5

第一个元素 (front): 10

使用 at(0): 10

使用 [0]: 10

Code analysis:

  1. numbers.front() returns the first element 10.
  2. It returns the same value as numbers.at(0) and numbers[0] , but with clearer semantics.

Example 2: Modifying the first element's value

front returns a reference, so it can be used to modify the element's value.

Example

#include

#include

#include

int

main

(

)

{

    std

::

vector

<

std

::

string

names

=

{

"Alice"

,

"Bob"

,

"Charlie"

}

;

    std

::

cout

<<

"Before modification, first element: "

<<

names.

front

(

)

<<

std

::

endl

;

   

// Use front() to get a reference and modify the element

    names.

front

(

)

=

"Andrew"

;

    std

::

cout

<<

"After modification, first element: "

<<

names.

front

(

)

<<

std

::

endl

;

   

// Now the content of names is: Andrew, Bob, Charlie

   

return

0

;

}

Expected output:

修改前,第一个元素: Alice

修改后,第一个元素: Andrew

Code analysis:

Example 3: Safe use of front - check whether the container is empty

When using front Before using, you should first check whether the container is empty.

Example

#include

#include

void

printFront

(

const

std

::

vector

<

int

&

v

)

{

if

(

!

v.

empty

(

)

)

{

        std

::

cout

<<

"The first element is: "

<<

v.

front

(

)

<<

std

::

endl

;

   

}

   

else

{

        std

::

cout

<<

"Container is empty; cannot get the first element"

<<

std

::

endl

;

   

}

}

int

main

(

)

{

    std

::

vector

<

int

numbers

=

{

10

,

20

,

30

}

;

   

// Normal case

    printFront

(

numbers

)

;

   

// Empty container case

    std

::

vector

<

int

emptyVec

;

    printFront

(

emptyVec

)

;

   

return

0

;

}

Expected output:

第一个元素是: 10

容器为空,无法获取第一个元素

Code analysis:

Example 4: Use front to implement the queue's head

front can be used to implement a queue-like data structure.

Example

#include

#include

#include

class

SimpleQueue

{

private

:

    std

::

vector

<

std

::

string

data

;

public

:

   

// Enqueue

   

void

enqueue

(

const

std

::

string

&

item

)

{

        data.

push_back

(

item

)

;

   

}

   

// Dequeue (remove the head)

   

void

dequeue

(

)

{

       

if

(

!

data.

empty

(

)

)

{

           

// Remove the first element; subsequent elements shift forward

            data.

erase

(

data.

begin

(

)

)

;

       

}

   

}

   

// Get the head element

    std

::

string

front

(

)

const

{

       

if

(

!

data.

empty

(

)

)

{

           

return

data.

front

(

)

;

       

}

       

return

""

;

   

}

   

// Check if the queue is empty

   

bool

empty

(

)

const

{

       

return

data.

empty

(

)

;

   

}

   

// Get queue size

   

size_t

size

(

)

const

{

       

return

data.

size

(

)

;

   

}

}

;

int

main

(

)

{

    SimpleQueue q

;

    q.

enqueue

(

"UserA"

)

;

    q.

enqueue

(

"UserB"

)

;

    q.

enqueue

(

"UserC"

)

;

    std

::

cout

<<

"Queue size: "

<<

q.

size

(

)

<<

std

::

endl

;

   

while

(

!

q.

empty

(

)

)

{

        std

::

cout

<<

"Processing: "

<<

q.

front

(

)

<<

std

::

endl

;

        q.

dequeue

(

)

;

   

}

    std

::

cout

<<

"Queue cleared"

<<

std

::

endl

;

   

return

0

;

}

Expected output:

队列大小: 3

处理: 用户A

处理: 用户B

处理: 用户C

队列已清空

Code analysis:


C++ Container Class The C++ container class