Site Tools


wiki:hpp-array

Table of Contents

<array>

<array> provides std::array<T, N>, a fixed-size array wrapper around a C array. It behaves like a container with .begin(), .end(), .size(), but with zero runtime overhead compared to a raw array.

Use it whenever you want the safety of STL containers (bounds checking with .at(), iterators) on a fixed-size block, without dynamic allocation.

Example

This example creates a fixed-size array, sorts it using standard algorithms, and accesses elements with both raw indexing and bounds-checked .at().

// compile: g++ -std=c++11 -o arrexample arrexample.cpp
// run: ./arrexample
// description: use std::array with container operations
 
#include <array>
#include <algorithm>
#include <iostream>
 
int main() {
    std::array<int, 5> arr = {3, 1, 4, 1, 5};
 
    std::sort(arr.begin(), arr.end());
 
    std::cout << "size: " << arr.size() << "\n";
    std::cout << "sorted: ";
    for (int x : arr) std::cout << x << " ";
    std::cout << "\n";
 
    std::cout << "at index 2: " << arr.at(2) << "\n";
 
    return 0;
}
wiki/hpp-array.md · Last modified: by 127.0.0.1