简体   繁体   English

STL容器类:数组

[英]STL container class: array

I am just exploring the STL containers in C++. 我只是在探索C ++中的STL容器。 Have some questions... There are two member functions max_size() and size(). 有一些问题...有两个成员函数max_size()和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. 我最初以为max_size()是数组的实际大小,而size()是显式存储的元素数。 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)? 有什么地方可以像Java一样阅读容器的设计规范(不是功能说明)? 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. 我似乎有很多问题,例如为什么std :: array的大小是固定的,但是却不能像std :: vector这样动态地更改等。显然,这种设计决定有一个或另一个原因。 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. 我有一个旧的“有效STL”,其中不包含std :: array。 I believe Scott meyers is yet to include std::array in it. 我相信Scott meyers尚未在其中包含std :: array。

As specified in the standard, max_size() is the maximum number of elements that the container can possibly store: 按照标准中的规定, max_size()是容器可能存储的最大元素数:

distance(begin(), end()) for the largest possible container 用于最大可能容器的distance(begin(), end())

(C++11, [container.requirements.general], table 96) (C ++ 11,[container.requirements.general],表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). 对于array是固定大小的容器,它与size()一致,而对于动态容器则完全不同,例如std::vector (它将返回类似于虚拟地址空间大小除以大小的内容)的元素)。

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). 您可以在C ++标准中找到容器的所有规范(价格昂贵,但其草案可在网上免费获得),尽管它只是一个规范性规范,往往无法很好地解释某些决策背后的原理(并且故意含糊不清)关于容器的实施)。

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. 尽管如此,之间的差std::arraystd::vector ,这是因为std::array旨在向存储元件不诉诸堆,提供相同性能的替代本地C数组,而std::vector使用堆存储元素,这可以提供更大的灵活性,但要付出一定的代价。 See this answer of mine for a more detailed comparison between std::array and std::vector . 请参阅我的答案,以更详细地比较std::arraystd::vector

The max_size method of std::array only exists to make it look like the other STL containers. 仅存在std::arraymax_size方法,以使其看起来与其他STL容器类似。 By having a common interface with other containers, much of the same code can be used for array s, vector s and list s. 通过与其他容器具有公共接口,许多相同的代码可用于array s, vector s和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 . 当然,由于array<T, N>size是该类型的一部分,因此sizemax_size必须都返回相同的值N

Re: I initially thought max_size() is the actual size of the array and size() is the number of elements explicitly stored. 回复:我最初以为max_size()是数组的实际大小,而size()是显式存储的元素数。

I think u confused vector::capacity() with max_size(). 我认为您将vector :: capacity()与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):) 在我的计算机(32位)中,对于int类型,max_size = 1073741823,对于双精度类型,max_size = 536870911。这意味着4G = 2 ^ 32 = 1073741823 * sizeof(int)= 536870911 * sizeof(double)。因此,max_size =您可以在电脑中使用的最大尺寸(无用!仅用于兼容):)

Re: Is there any place where I can read the design specs of containers like in Java(not the functional description)? 回复:有什么地方可以阅读Java之类的容器设计规范(不是功能描述)? Maybe >The Annotated STL Sources< is not best for u,but it's a good book for STL. 也许> Annotated STL Sources <对您而言不是最好的选择,但对于STL来说却是一本好书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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