::fast_io::array
1. What is an array?
A ::fast_io::array<T, N> stores exactly N elements of type T.
The size is fixed at compile time and never changes.
Because the size is known ahead of time, the array stores its elements directly inside the object itself. There is no dynamic allocation and no resizing.
For most situations, you should prefer ::fast_io::vector<T> because it grows automatically.
::fast_io::array is useful when the size is truly fixed and known in advance.
2. Creating an array
You specify both the element type and the number of elements:
#include <fast_io.h>
#include <fast_io_dsal/array.h>
int main() {
using namespace ::fast_io::iomnp;
::fast_io::array<std::size_t, 3zu> a{1zu, 2zu, 3zu};
println("a[0] = ", a[0]);
}
If you do not provide initial values, the elements are default‑initialized:
::fast_io::array<std::size_t, 5zu> b{}; // all elements default-initialized
3. Accessing elements
Access works the same way as with ::fast_io::vector:
#include <fast_io.h>
#include <fast_io_dsal/array.h>
int main() {
using namespace ::fast_io::iomnp;
::fast_io::array<std::size_t, 4zu> a{10zu, 20zu, 30zu, 40zu};
println("Front: ", a.front()); // 10
println("Back: ", a.back()); // 40
println("Index 2: ", a[2]); // 30
}
⚠️ Safety note: If you use an out‑of‑range index, fast_io calls
fast_terminate() immediately.
Safe usage
std::size_t i = 2zu;
if(i < a.size()) {
println("a[2] = ", a[i]);
}
4. Iterating through elements
Range‑based for loop
Just like strings and vectors, arrays support range‑based for loops:
for(std::size_t x : a) {
println(x);
}
Index loop
for(std::size_t i{}, n{a.size()}; i != n; ++i) {
println("Index ", i, ": ", a[i]);
}
Advanced: Iterators
Arrays use the same iterator model introduced in 3.1 (string) and used again in 4.2 (vector). Iterators:
- are contiguous
- support arithmetic (
+/-,[], comparisons) - use the left‑inclusive, right‑exclusive range
[begin(), end())
for(auto it = a.begin(), ed = a.end(); it != ed; ++it) {
println(*it);
}
Because the size is fixed and no reallocation ever happens, array iterators never become invalid.
5. Utility functions
fill()
Sets all elements to the same value:
a.fill(7zu); // all elements become 7
swap()
Swaps the contents of two arrays of the same size:
::fast_io::array<std::size_t, 3zu> x{1zu, 2zu, 3zu};
::fast_io::array<std::size_t, 3zu> y{9zu, 8zu, 7zu};
x.swap(y);
6. Copying arrays
::fast_io::array<T, N> supports copying just like any other
trivially copyable container. Because the size is fixed and all elements are
stored directly inside the object, copying an array always copies all
N elements.
Copy construction
::fast_io::array<std::size_t, 3zu> a{1zu, 2zu, 3zu};
::fast_io::array<std::size_t, 3zu> b{a}; // copy constructor
Copy assignment
::fast_io::array<std::size_t, 3zu> a{1zu, 2zu, 3zu};
::fast_io::array<std::size_t, 3zu> b{};
b = a; // copy assignment
Copying between arrays of different sizes
Arrays must have the same size N to be copied. The following is
a compile‑time error:
::fast_io::array<std::size_t, 3zu> a{};
::fast_io::array<std::size_t, 4zu> b{};
// b = a; // ❌ compile‑time error: sizes differ
Copying with iterators
::fast_io::array<std::size_t, 3zu> a{1zu, 2zu, 3zu};
::fast_io::array<std::size_t, 3zu> b{};
for(std::size_t i{}; i != a.size(); ++i) {
b[i] = a[i];
}
Since arrays never reallocate, iterator‑based copying is always safe.
Key takeaways
::fast_io::array<T, N>stores exactlyNelements with no dynamic allocation.- The size is fixed at compile time and cannot grow or shrink.
- Access functions (
front(),back(),operator[]) behave like those in vectors. - Array iterators behave exactly like string and vector iterators, but never become invalid.
fill()andswap()provide convenient ways to modify the entire array.- Copying an array always copies all elements; arrays of different sizes cannot be copied.
- For most tasks, prefer
::fast_io::vector<T>— it is the default container and grows automatically.