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;
Call
What 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.
begin() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Iterator
An iterator to the first element. On a const array it is a const_iterator.
Empty array
For 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;
}
Output
5 10 15 20
How It Works
auto it = nums.begin() starts the iterator on the first element.
*it reads the current value, and ++it moves to the next one.
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;
}
Output
100 25 30
How It Works
*it = 100 replaces the first element, which was 1.
++it moves the iterator to the second element, 20.
*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;
}
Output
1 2 3 4
How It Works
std::sort needs to know where the range starts and where it stops.
nums.begin() is the start, and nums.end() is the stop mark.
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.