In C++, std::array::back() returns the last element of a std::array. You do not need to calculate size() - 1 yourself.
It returns a reference, so you can read the value or change it. The array must have at least one element. Include <array> to use it.
This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.
Syntax
Call back with empty brackets. It takes no arguments.
C++
nums.back();
nums.back() = 99;
Call
What it does
nums.back()
Reads the last element.
nums.back() = 99
Replaces the last element with a new value.
The first element has a matching function: front().
Library declarations
C++
reference back();
const_reference back() const;
Parameters
Parameter
Description
None
back() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Reference
A reference to the last element, which is the same as nums[nums.size() - 1].