Search code examples
c++stl

STL container class: array


I am just exploring the STL containers in C++. Have some questions... There are two member functions max_size() and size(). They seem to be doing the same thing. I initially thought max_size() is the actual size of the array and size() is the number of elements explicitly stored. But as I tested it out, it's not the case. Then why two different functions?

Is there any place where I can read the design specs of containers like in Java (not the functional description)? I seem to have many questions such as why the std::array size is fixed but can't be changed dynamically like std::vector etc. Obviously there has to one reason or another for such design decisions. It would be useful to read such design specs to understand such limitations. I have one old "Effective STL" which doesn't include std::array. I believe Scott meyers is yet to include std::array in it.


Solution

  • The max_size method of std::array only exists to make it look like the other STL containers. By having a common interface with other containers, much of the same code can be used for arrays, vectors and lists.

    Of course, since the size of an array<T, N> is part of the type, the size and max_size must both return the same value, N.