Fixed-Size std::array
A safe C-array replacement.
A Safe C-Array
std::array<T, N> is a fixed-size array that knows its own size and behaves like a proper container. Include <array>.
#include <iostream>
#include <array>
int main() {
std::array<int, 3> a = {1, 2, 3};
std::cout << a[0] << " " << a.size() << "\n";
}Knows Its Size
Unlike a raw array that decays to a pointer, std::array always reports its length via size().
#include <iostream>
#include <array>
int main() {
std::array<double, 4> a{};
std::cout << a.size() << "\n"; // 4
}All lessons in this course
- Fixed-Size std::array
- std::span Basics
- Passing Spans to Functions
- Bounds and Subspans