C++ std::array back() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::array

Definition

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;
CallWhat it does
nums.back()Reads the last element.
nums.back() = 99Replaces 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

ParameterDescription
Noneback() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
ReferenceA reference to the last element, which is the same as nums[nums.size() - 1].

C++ Examples

Example 1 — Basic back()

Print the last element of a small array.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 4> nums = {3, 6, 9, 12};

    std::cout << "Last element: " << nums.back() << '\n';
    return 0;
}

How It Works

  1. The array holds four numbers, and 12 is stored at the end.
  2. back() returns a reference to that last slot.
  3. cout prints the value, so the output is 12.

Example 2 — Change the Last Element

Assign to back() to replace the last value.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 3> nums = {1, 2, 3};

    nums.back() = 99;

    for (int n : nums) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. back() is a reference, so it can sit on the left side of =.
  2. Only the last slot changes from 3 to 99.
  3. The range-based for loop prints every element, which shows the new value.

Example 3 — back() on a Const Array

A function that only reads the array can still call back().

C++
#include <array>
#include <iostream>

void showLast(const std::array<int, 4>& readings) {
    std::cout << "Last reading: " << readings.back() << '\n';
}

int main() {
    std::array<int, 4> data = {18, 21, 25, 23};
    showLast(data);
    return 0;
}

How It Works

  1. The parameter is a const reference, so the function cannot change the array.
  2. On a const array, back() returns a const_reference. You can read it, but not assign to it.
  3. The size 4 is part of the type, so the function only accepts arrays of exactly four ints.

Frequently Asked Questions

It returns a reference to the last element of the array. That is the element at index size() - 1.
No. A std::array<T, 0> has no elements, so calling back() on it is undefined behavior. Check empty() first if the size can be zero.
back() gives you the last element itself. end() gives an iterator that points just after the last element. You must not dereference end().
Yes, on a non-const array. back() returns a reference, so nums.back() = 5; works.
Yes, for a non-empty array. back() is shorter and easier to read.

Next: begin()

Get an iterator to the first element with std::array.

begin() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful