C++ std::array begin() Function

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

Definition

In C++, std::array::begin() returns an iterator that points to the first element of the array. An iterator works like a pointer: *it reads the element it points to.

You usually use it together with end() in a loop, or pass the pair to algorithms such as std::sort. 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 begin with empty brackets. It takes no arguments.

C++
auto it = nums.begin();
*it = 100;
CallWhat it does
nums.begin()Returns an iterator to the first element.
*nums.begin()Reads the first element through the iterator. You can also assign to it.

If you only want to read, use cbegin(). It always gives a read-only iterator.

Library declarations
C++
iterator       begin() noexcept;
const_iterator begin() const noexcept;

Parameters

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

Return Value

Return ValueDescription
IteratorAn iterator to the first element. On a const array it is a const_iterator.
Empty arrayFor std::array<T, 0>, begin() is equal to end(), and you must not dereference it.

C++ Examples

Example 1 — Loop with begin()

Walk through every element with an iterator.

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

int main() {
    std::array<int, 4> nums = {5, 10, 15, 20};

    for (auto it = nums.begin(); it != nums.end(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. auto it = nums.begin() starts the iterator on the first element.
  2. *it reads the current value, and ++it moves to the next one.
  3. The loop stops when it reaches nums.end().

Example 2 — Change Values Through the Iterator

A non-const iterator lets you write to the element.

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

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

    auto it = nums.begin();
    *it = 100;
    ++it;
    *it += 5;

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

How It Works

  1. *it = 100 replaces the first element, which was 1.
  2. ++it moves the iterator to the second element, 20.
  3. *it += 5 adds 5 to that element, so it becomes 25.

Example 3 — Sort with begin() and end()

Algorithms take a pair of iterators. Here std::sort sorts the whole array.

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

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

    std::sort(nums.begin(), nums.end());

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

How It Works

  1. std::sort needs to know where the range starts and where it stops.
  2. nums.begin() is the start, and nums.end() is the stop mark.
  3. The values inside the same array are rearranged in place, so the array now reads 1 2 3 4.

Frequently Asked Questions

It returns an iterator that points to the first element of the array. For a const array it returns a const_iterator.
front() returns the first element itself. begin() returns an iterator that points to it. You write *nums.begin() to get the same value as nums.front().
Yes. It returns a valid iterator that equals end(). You must not dereference it, because there is no element.
A std::array has a fixed size and never reallocates, so its iterators stay valid as long as the array object exists. After a swap, an iterator still points into the same array, but the values in it have changed.
For a plain loop over every element, a range-based for loop is simpler. Use begin() when you need an algorithm such as std::sort, or when you need to move the iterator yourself.

Next: cbegin()

Get a read-only iterator to the first element with std::array.

cbegin() 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