C++ · Module 6 · Lesson 21 min read

std::array

Declaring (<array>)

c
std::array<int, 5> marks = { 80, 75, 92, 68, 88 };
std::array<double, 12> monthly{};    // 12 zeros
std::array days = { "Sun", "Mon" };  // C++17 deduces std::array<const char*, 2>

The size is part of the type: std::array<int, 5> and std::array<int, 6> are different types.

Useful members

MemberMeaning
a.size()number of elements
a[i]element (no check)
a.at(i)element with bounds check (throws std::out_of_range)
a.front(), a.back()first / last
a.fill(x)set all elements to x
a.data()pointer to the raw array
a.begin(), a.end()iterators (for algorithms)

Advantages over C arrays

  • ✅ Knows its size (size()), even inside functions
  • ✅ Copy with =, compare with == and <
  • ✅ Can be returned from functions
  • ✅ Works with STL algorithms (std::sort(a.begin(), a.end()))
  • ✅ Same performance and memory layout as a C array (lives on the stack)

Passing to functions

c
void print(const std::array<int, 5>& a);   // size fixed by the type
template <std::size_t N> void print(const std::array<int, N>& a);   // any size

Or use std::span<const int> (C++20) to accept arrays, std::arrays and vectors alike.

When to use

  • Size known at compile time and never changes: months, days, board cells, RGB channels → std::array
  • Size changes or unknown → std::vector

Structured bindings

auto [r, g, b] = color; works with std::array too.

Example

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

constexpr std::array<const char*, 12> MONTHS = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

double yearly_total(const std::array<double, 12>& spent) {
    double t = 0;
    for (double x : spent) t += x;
    return t;
}

int main() {
    std::array<double, 12> spent{};                 // all zeros
    spent[0] = 21000; spent[1] = 19500; spent[10] = 24800; spent[11] = 31000;

    std::cout << "Tracked " << spent.size() << " months, total Rs. " << yearly_total(spent) << '\n';
    auto max_it = std::max_element(spent.begin(), spent.end());
    std::cout << "Most expensive month: " << MONTHS[max_it - spent.begin()] << " (Rs. " << *max_it << ")\n";

    std::array<int, 5> marks = { 68, 92, 75, 88, 80 };
    std::array<int, 5> backup = marks;               // copy with =
    std::sort(marks.begin(), marks.end());
    std::cout << "sorted:";
    for (int m : marks) std::cout << ' ' << m;
    std::cout << "\nfront " << marks.front() << ", back " << marks.back()
              << ", equal to backup? " << std::boolalpha << (marks == backup) << '\n';

    try {
        std::cout << marks.at(7) << '\n';
    } catch (const std::out_of_range& e) {
        std::cout << "at(7) threw out_of_range\n";
    }

    std::array<int, 3> rgb = { 255, 165, 0 };
    auto [r, g, b] = rgb;
    std::cout << "orange = rgb(" << r << ", " << g << ", " << b << ")\n";
    return 0;
}

/* Output:
Tracked 12 months, total Rs. 96300
Most expensive month: Dec (Rs. 31000)
sorted: 68 75 80 88 92
front 68, back 92, equal to backup? false
at(7) threw out_of_range
orange = rgb(255, 165, 0)
*/