To emphasize a point made by @MatteoItalia, the efficiency difference is where the data is stored. Heap memory (required with vector
) requires a call to the system to allocate memory and this can be expensive if you are counting cycles. Stack memory (possible for array
) is virtually "zero-overhead" in terms of time, because the memory is allocated by just adjusting the stack pointer and it is done just once on entry to a function. The stack also avoids memory fragmentation. To be sure, std::array
won't always be on the stack; it depends on where you allocate it, but it will still involve one less memory allocation from the heap compared to vector. If you have a
definitely use a std::array
over a vector. If any of those requirements is not true, then use a std::vector
.