C++ std::array size() Function

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

Definition

In C++, std::array::size() returns the number of elements in the array. For std::array<T, N> the answer is always N.

Unlike a plain C array, a std::array remembers its own size, so you never need sizeof(arr) / sizeof(arr[0]). 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 size with empty brackets. It takes no arguments.

C++
std::size_t count = nums.size();
for (std::size_t i = 0; i < nums.size(); ++i) { }
CallWhat it does
nums.size()Returns the number of elements.
i < nums.size()A common loop condition that keeps the index inside the array.

The result type is std::size_t, which is unsigned. Use it for loop counters to avoid warnings.

Library declarations
C++
constexpr size_type size() const noexcept;

Parameters

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

Return Value

Return ValueDescription
SizeThe number of elements, N, of type size_type.

C++ Examples

Example 1 — Basic size()

Print the size of two arrays with different types.

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

int main() {
    std::array<int, 5> nums = {1, 2, 3, 4, 5};
    std::array<double, 3> prices = {9.5, 12.0, 7.25};

    std::cout << "nums.size(): " << nums.size() << '\n';
    std::cout << "prices.size(): " << prices.size() << '\n';
    return 0;
}

How It Works

  1. The second template value in std::array<int, 5> is the size.
  2. nums.size() returns that number, 5.
  3. prices was declared with 3, so its size is 3 even though it stores doubles.

Example 2 — Use size() in a Loop

Let the array tell the loop where to stop.

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

int main() {
    std::array<int, 4> nums = {2, 4, 6, 8};
    int total = 0;

    for (std::size_t i = 0; i < nums.size(); ++i) {
        total += nums[i];
    }

    std::cout << "Total: " << total << '\n';
    return 0;
}

How It Works

  1. The counter i starts at 0 and has the type std::size_t.
  2. The loop keeps going while i < nums.size(), so i goes through 0, 1, 2, and 3 only.
  3. The loop adds 2 + 4 + 6 + 8, which gives 20.

Example 3 — size() at Compile Time

Because size() is constexpr, you can use it where a constant is required.

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

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

    static_assert(a.size() == 4, "a must have 4 items");

    std::array<int, a.size()> b{};

    std::cout << "Second array size: " << b.size() << '\n';
    return 0;
}

How It Works

  1. a is a constexpr array, so a.size() is known during compilation.
  2. static_assert checks the size while compiling. The program would not build if it were wrong.
  3. a.size() is also used as the size of b, so both arrays have 4 elements.

Frequently Asked Questions

It returns the number of elements in the array. For std::array<T, N> that is always N.
size() counts elements. sizeof gives the number of bytes. For std::array<int, 5>, size() is 5 but sizeof is usually 20.
No. The size is part of the type and is fixed. If you need a size that changes, use std::vector.
The return type is size_type, which is unsigned. Use std::size_t for loop counters so you do not get sign-compare warnings.
For std::array, yes. Both return N.

Next: swap()

Swap the contents with another array of the same type and size with std::array.

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

13 people found this page helpful