简体   繁体   English

固定容量的自定义STL容器的max_size()的正确值是什么?

[英]What is the correct value of max_size() for a fixed capacity custom STL container?

I'm writing a custom circular buffer implementation with fixed capacity (fixed at runtime by call to constructor, no resize methods) and want it to be STL-compatible. 我正在编写一个具有固定容量的自定义循环缓冲区实现(在运行时通过调用构造函数进行固定,没有调整大小的方法),并希望它与STL兼容。 My goal is to make it a Random Access Container . 我的目标是使其成为一个随机访问容器 Additionally I want it to have the specific interface of an Back Insertion Sequence , but without being a sequence (no resize and inserting/erasing at arbitrary positions, ...). 另外,我希望它具有Back Insertion Sequence的特定接口,但又不是一个序列(不能在任意位置调整大小和插入/擦除...)。 So it would only be an extended Random Access Container. 因此,它只会是扩展的随机访问容器。 After reading some questions about max_size() and size() , I'm still a little confused. 在阅读了有关max_size()size()的一些问题之后,我仍然有些困惑。

My current idea: size() : number of elements contained in buffer 我当前的想法: size() :缓冲区中包含的元素数
max_size() : capacity of buffer (maximum number of elements it can hold) max_size() :缓冲区的容量(它可以容纳的最大元素数)

Is this correct (conforming to standard / STL)? 这是正确的吗(符合标准/ STL)? Or do I have to handle it like (std::)array with size() == max_size()? 还是我必须像size()== max_size()的(std::) array那样处理它?

The standard is quite clear on the meaning of max_size() in C++11 Table 96: 该标准对于C ++ 11表96中max_size()的含义非常明确:

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

If, like std::array , the size is a property of the container type (eg specified by a template parameter), then that should be the same as size() . 如果像std::array ,大小是容器类型的属性(例如,由模板参数指定),则该size()应与size()相同。 If you can instantiate the same type with various sizes, then it should be the largest allowed size. 如果可以实例化具有各种大小的同一类型,则它应该是允许的最大大小。

I would follow the example of the standard containers, and have a capacity() function to tell you the capacity. 我将遵循标准容器的示例,并具有一个capacity()函数来告诉您容量。

max_size doesn't relate to a specific instance of the container type. max_size与容器类型的特定实例无关。 It is the maximum size that the type can handle, according to whatever implementation details limit it (the max value of size_type if nothing else). 根据实现细节的限制,它是该类型可以处理的最大大小(如果没有其他限制,则为size_type的最大值)。

vector can reserve space that isn't currently used, and for that reason it has a capacity() function. vector可以保留当前不使用的空间,因此它具有capacity()函数。 This isn't part of any of the container concepts though, because other containers ( deque , list ) don't reserve space. 但是,这不是任何容器概念的一部分,因为其他容器( dequelist )不会保留空间。

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

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