简体   繁体   中英

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.

As specified in the standard, max_size() is the maximum number of elements that the container can possibly store:

distance(begin(), end()) for the largest possible container

(C++11, [container.requirements.general], table 96)

For array , which is a fixed-size container, it coincides with size() , while it is completely different for dynamic containers, like std::vector (where it will return something like the size of the virtual address space divided by the size of the element).

You can find all the specifications of the containers in the C++ standard (which is quite expensive, but its drafts are freely available online), although it's just a normative specification which tends not to explain well the rationale behind some decisions (and is deliberately vague on the implementation of the containers).

Still, for the difference between std::array and std::vector , it's because std::array is intended to store the elements without resorting to the heap, providing a same-performance alternative to a local C-style array, while std::vector uses the heap to store the elements, which allows much more flexibility but comes at a cost. See this answer of mine for a more detailed comparison between std::array and std::vector .

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 array s, vector s and list s.

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 .

Re: I initially thought max_size() is the actual size of the array and size() is the number of elements explicitly stored.

I think u confused vector::capacity() with max_size(). And in my computer(32-bit),for int type max_size=1073741823,for double type max_size=536870911.which means 4G=2^32=1073741823*sizeof(int)=536870911*sizeof(double)。 So, max_size=the max size u can used in ur computer(useless!just for compatible):)

Re: Is there any place where I can read the design specs of containers like in Java(not the functional description)? Maybe >The Annotated STL Sources< is not best for u,but it's a good book for STL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM