C++ back() function
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
- Parameters: No parameters
backtakes no arguments.
Function description
- Return Value: Returns thereferenceto the element at the specified position. If the container is const, a const reference is returned.
- effect: Returns the last element of the container (index
size()-1element). - Note: array cannot be empty, so calling
backis safe.
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:
numbers.back()returns the last element50.- It returns the same value as
numbers.at(numbers.size()-1)andnumbers[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:
tasks.back() = "完成项目";modified the value of the last element through the reference.back()returns a modifiable lvalue reference.
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:
- After sorting,
front()returns the minimum value,back()returns the maximum value. - which is a simple way to get the max/min values.
The C++ container class