cpp / intermediate
Snippet
Fixed-Size Containers with Standard Iterators
Unlike raw C-style arrays, std::array is a first-class container that doesn't decay to a pointer automatically. It knows its own size and supports the standard iterator interface (begin/end), making it compatible with all standard algorithms while maintaining zero-overhead performance.
snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>#include <array>#include <algorithm>int main() {// std::array is a wrapper around a raw arraystd::array<int, 5> buffer = {42, 10, 5, 30, 22};// Supports standard iterators and algorithmsstd::sort(buffer.begin(), buffer.end());for (const int val : buffer) {std::cout << val << " ";}return 0;}
Breakdown
1
std::array<int, 5> buffer = {42, 10, 5, 30, 22};
Defines a fixed-size array of 5 integers. The size is part of the type.
2
std::sort(buffer.begin(), buffer.end());
Uses standard library sorting on the fixed-size container.