Whenever I try with max_size()
and size()
funtion of std::array
, I get same results, I wanted to know if there could be a situation where two of them give different results.
std::array is a container that encapsulates fixed size arrays. This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member. Unlike a C-style array, it doesn't decay to T* automatically.
It will provide a value like semantics equally to the other C++ containers. A std::array should have same runtime performance as a c-style array.
std::array is just a class version of the classic C array. That means its size is fixed at compile time and it will be allocated as a single chunk (e.g. taking space on the stack). The advantage it has is slightly better performance because there is no indirection between the object and the arrayed data.
Yes the memory of std::array is contiguous.
Those two functions will always return the same value, namely N
for an std::array<T, N>
.
They are provided for consistency with other containers, for which the values will differ (e.g. std::vector
).
That function exists for compatibility with other containers like std::vector
. For std::array
those two values will always be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With