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 array,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 and initialize an array

    std

::

array

<
int
,
5
numbers
=
{
10
,
20
,
30
,
40
,
50
}

;

    std

::

cout

<<

"array 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:

array的大小是: 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

::

array

<

std

::

string

,

3

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

;

   

return

0

;

}

Expected output:

修改前,第一个元素: Alice
修改后,第一个元素: Andrew

Code analysis:

Example 3: Using front to implement a queue's head

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

Example

#include

#include

#include

// Simple fixed queue (implemented with array)

class

FixedQueue

{

private

:

    std

::

array

<

std

::

string

,

100

data

;

   

size_t

frontIndex

=

0

;

   

size_t

backIndex

=

0

;

   

size_t

count

=

0

;

public

:

   

// Enqueue

   

bool

enqueue

(

const

std

::

string

&

item

)

{

       

if

(

count

=

100

)

return

false

;

        data

[

backIndex

]

=

item

;

        backIndex

=

(

backIndex

1

)

%

100

;

       

++

count

;

       

return

true

;

   

}

   

// Get the head element

    std

::

string

front

(

)

const

{

       

return

data

[

frontIndex

]

;

   

}

   

// Check if the queue is empty

   

bool

empty

(

)

const

{

       

return

count

==

0

;

   

}

   

// Get queue size

   

size_t

size

(

)

const

{

       

return

count

;

   

}

}

;

int

main

(

)

{

    FixedQueue 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

;

   

}

   

return

0

;

}

Expected output:

队列大小: 3
处理: 用户A
处理: 用户B
处理: 用户C

Code analysis:


C++ Container Class The C++ container class