C++ std::array at() Function

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

Definition

In C++, std::array::at() gives you the element stored at one index of a std::array. Counting starts at 0, so the first element is index 0 and the last one is index size() - 1.

The special thing about at() is that it checks the index. If the index is too big, it throws std::out_of_range instead of reading random memory. 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 at on the array you already have. Pass one index inside the brackets.

C++
nums.at(2);
nums.at(2) = 50;
CallWhat it does
nums.at(2)Reads the element at index 2. The index is checked first.
nums.at(2) = 50Changes the element at index 2. This works because at() returns a reference.

Square brackets also work: nums[2]. The difference is that [] does not check the index.

Library declarations
C++
reference       at(size_type pos);
const_reference at(size_type pos) const;

Parameters

ParameterDescription
posRequired. The index of the element, counting from 0. It must be less than size().

Return Value

Return ValueDescription
ReferenceA reference to the element at index pos. You can read it or assign to it. For a const array the reference is read-only.
ExceptionThrows std::out_of_range when pos >= size().

C++ Examples

Example 1 — Basic at()

Read two elements by index.

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

int main() {
    std::array<int, 5> marks = {70, 80, 90, 60, 85};

    std::cout << "marks.at(0) = " << marks.at(0) << '\n';
    std::cout << "marks.at(2) = " << marks.at(2) << '\n';
    return 0;
}

How It Works

  1. marks is an array of 5 ints, with indexes 0 to 4.
  2. at(0) returns the first value, 70.
  3. at(2) returns the third value, 90, because counting starts at 0.

Example 2 — Bad Index (out_of_range)

Use a try / catch block to handle a bad index safely.

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

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

    try {
        std::cout << nums.at(5) << '\n';
    } catch (const std::out_of_range&) {
        std::cout << "Caught: index 5 is out of range\n";
    }
    return 0;
}

How It Works

  1. The array has only 3 elements, so the valid indexes are 0, 1, and 2.
  2. nums.at(5) checks the index and throws std::out_of_range.
  3. The catch block runs, so the program prints a message instead of crashing.

Example 3 — Change Values with at()

Because at() returns a reference, you can assign to it.

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

int main() {
    std::array<int, 4> scores = {0, 0, 0, 0};

    scores.at(1) = 95;
    scores.at(3) = 88;

    for (std::size_t i = 0; i < scores.size(); ++i) {
        std::cout << "scores[" << i << "] = " << scores.at(i) << '\n';
    }
    return 0;
}

How It Works

  1. All four scores start at 0.
  2. scores.at(1) = 95 writes straight into index 1 because at() gives back a reference.
  3. The loop uses size() as its limit, so every at(i) call stays inside the array.

Frequently Asked Questions

It returns the element stored at a given index of a std::array. It checks the index first and throws std::out_of_range if the index is too large.
Both read an element by index. at() checks the index and throws std::out_of_range for a bad one. operator[] does no check, so a bad index is undefined behavior. operator[] can be slightly faster.
Yes. at() returns a reference, so nums.at(1) = 10; changes the element. For a const array it returns a const reference, so you can only read.
The index type is size_type, which is unsigned. A negative number turns into a very large value, so at() throws std::out_of_range.
Yes. std::vector::at() works in the same way. The difference is that a std::array has a fixed size known at compile time, while a vector can grow.

Next: back()

Get the last element of the array with std::array.

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

7 people found this page helpful