C++ std::array front() Function

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

Definition

In C++, std::array::front() returns the first element of a std::array. It is the same element as index 0.

It returns a reference, so you can read it or replace it. Like back(), it needs an array with 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 front with empty brackets. It takes no arguments.

C++
nums.front();
nums.front() = 50;
CallWhat it does
nums.front()Reads the first element.
nums.front() = 50Replaces the first element with a new value.

The last element has a matching function: back().

Library declarations
C++
reference       front();
const_reference front() const;

Parameters

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

Return Value

Return ValueDescription
ReferenceA reference to the first element, which is the same as nums[0].

C++ Examples

Example 1 — Basic front()

Print the first element of an array.

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

int main() {
    std::array<int, 3> nums = {8, 16, 24};

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

How It Works

  1. The array stores 8 in its first slot.
  2. front() returns a reference to that slot.
  3. The output is 8, the same as nums[0].

Example 2 — Change the First Element

Assign to front() to replace the first value.

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

int main() {
    std::array<int, 3> nums = {5, 6, 7};

    nums.front() = 50;

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

How It Works

  1. Because front() returns a reference, it can be assigned a new value.
  2. Only index 0 changes, from 5 to 50.
  3. The loop shows that the other two elements, 6 and 7, stay the same.

Example 3 — Use front() and back() Together

Read both ends of the array and combine them.

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

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

    int first = nums.front();
    int last = nums.back();

    std::cout << "First: " << first << '\n';
    std::cout << "Last: " << last << '\n';
    std::cout << "Sum of ends: " << first + last << '\n';
    return 0;
}

How It Works

  1. front() reads 3, and back() reads 9.
  2. The values in the middle, 8 and 5, are never touched.
  3. The two ends are added, so 3 + 9 gives 12.

Frequently Asked Questions

It returns a reference to the first element of the array. That is the element at index 0.
front() gives the element itself. begin() gives an iterator that points to the element. *nums.begin() gives the same value as nums.front().
No. A std::array<T, 0> has no elements, so front() is undefined behavior there.
Yes, on a non-const array. nums.front() = 10; works because front() returns a reference.
Yes, for any array with at least one element. front() is easier to read.

Next: max_size()

Get the largest number of elements the array can hold with std::array.

max_size() 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.

9 people found this page helpful