限制 stl 的向量 max_size
如何限制STL向量的max_size?最终通过专业化。欢迎举个例子。
How can I limit STL vector's max_size? Eventually by specialization. An example would be welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现这一点的方法是替换分配器。请注意,向量和字符串是目前唯一实际检查其分配器 max_size 的容器。这个想法是,由于这些容器保证元素存储在连续的内存中,因此容器会询问分配器该分配器可以处理多少个元素。
这就是想法
,然后创建一个新的向量
我还没有尝试编译此代码,但它应该可以工作。
The way to do this is to replace the allocator. Note that vector and string are the only containers that actually check their allocators max_size right now. The idea is that since these containers guarantee that the elements are stored in contiguous memory, the container asks the allocator just how many elements the allocator can handle.
This is the idea
then make a new vector
I haven't tried compiling this code, but it should work.
std::vector
中的max_size
不是您可以更改的内容。它是特定于当前向量的常量,显示您可以在该向量中放入多少当前类型的元素(我猜这将取决于您的系统类型)因此,对于max_size 会有很大差异,就像
std::vector
和 std::vectorint
和double
不同一样。max_size
instd::vector
isn't something you can alter. It's a constant specific for current vector, that shows how many elements of current type you can put in that vector (I guess it will depend on your system type)So, for
std::vector<int>
andstd::vector<double>
max_size will differ that much, asint
anddouble
differ.