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

C++ back() function

C++ back() function

C++ Container Class The C++ container class


Among the various operations on array,back is a very practical function, used toget the last element.

back is a member function of the container class, used toreturns the last element of the container. It is equivalent to at(size()-1) or operator[](size()-1), but with clearer semantics.

back provides an intuitive way to access the container's last element.

Word meaning: back means 'back', i.e., to get the last (rearmost) element.


Basic syntax and parameters

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

Syntax format


reference back();
const_reference back() const;

Parameter Description

- `back` takes no arguments.

Function description


Example

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

Example 1: Basic usage - getting the last 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 back to get the last element

    std

::

cout

<<

"Last element (back): "

<<

numbers.

back

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Using at(size()-1): "

<<

numbers.

at

(

numbers.

size

(

)

1

)

<<

std

::

endl

;

    std

::

cout

<<

"Using [size()-1]: "

<<

numbers

[

numbers.

size

(

)

1

]

<<

std

::

endl

;

   

return

0

;

}

Expected output:

array的大小是: 5
最后一个元素 (back): 50
使用 at(size()-1): 50
使用 [size()-1]: 50

Code analysis:

  1. numbers.back() returns the last element 50.
  2. It returns the same value as numbers.at(numbers.size()-1) and numbers[numbers.size()-1] , but with clearer semantics.

Example 2: Modifying the last element's value

back 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

tasks

=

{

"Learn C++"

,

"Do homework"

,

"Read documentation"

}

;

    std

::

cout

<<

"Before modification, last task: "

<<

tasks.

back

(

)

<<

std

::

endl

;

   

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

    tasks.

back

(

)

=

"Complete project"

;

    std

::

cout

<<

"After modification, last task: "

<<

tasks.

back

(

)

<<

std

::

endl

;

   

return

0

;

}

Expected output:

修改前,最后一个任务: 阅读文档
修改后,最后一个任务: 完成项目

Code analysis:

Example 3: Using back with sort to get max and min values

After sorting,back() returns the maximum value.

Example

#include

#include

#include

int

main

(

)

{

    std

::

array

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

;

    std

::

cout

<<

"Before sorting: "

;

   

for

(

int

n

:

numbers

)

std

::

cout

<<

n

<<

" "

;

    std

::

cout

<<

std

::

endl

;

   

// Sort

    std

::

sort

(

numbers.

begin

(

)

, numbers.

end

(

)

)

;

    std

::

cout

<<

"After sorting: "

;

   

for

(

int

n

:

numbers

)

std

::

cout

<<

n

<<

" "

;

    std

::

cout

<<

std

::

endl

;

    std

::

cout

<<

"Minimum (front): "

<<

numbers.

front

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Maximum (back): "

<<

numbers.

back

(

)

<<

std

::

endl

;

   

return

0

;

}

Expected output:

排序前: 30 10 50 20 40
排序后: 10 20 30 40 50
最小值 (front): 10
最大值 (back): 50

Code analysis:


C++ Container Class The C++ container class