Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between T[N] and std::array<T,N> [duplicate]

Tags:

c++

arrays

c++11

My question is pretty straightforward, but to be more specific I want to quote 2 lines from Stroustrup11.

  1. T[N] A fixed-size built-in array: N contiguous elements of type T; no size() or other member functions
  2. array<T,N> A fixed-size array of N contiguous elements of type T; like the built-in array, but with most problems solved

So what is the difference the author is mentioning? And what problems are solved for std::array<T,N> ?

like image 353
Eduard Rostomyan Avatar asked Apr 22 '26 15:04

Eduard Rostomyan


1 Answers

The principal differences are that std::array<T, N> doesn't decay to a pointer to the first element where T[N] would, and you can take a value copy of a std::array<T, N>.

std::array also offers some useful functions, such as lexicographical comparison operators.

But because N has to be a compile time evaluable constant expression, std::vector<T> is often the preferred choice.

like image 162
Bathsheba Avatar answered Apr 25 '26 04:04

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!