有没有办法获得 deque 的内部存储大小作为 vector::capacity ?
据我了解,deque和vector都保留了一些增长空间。 vector::capacity() 能够获取向量的内部保留空间。双端队列标准中没有这样的成员。有什么方法可以获取这些信息吗?
I understand that both deque and vector reserve some space for growth. vector::capacity() is able to get the internal reserved space for a vector. Deque doesn't have such a member in the standard. Is there some way to get this information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须深入研究实现才能弄清楚这一点。 gcc 4.1.1 附带的 std::deque 版本似乎以 512 字节块分配内存。但这是我花了 15 分钟盯着所有下划线和 C 风格转换为
size_t
后得到的结果。然后我看到了这样的评论:You'd have to dig into the implementation to figure that out. The version of
std::deque
that comes with gcc 4.1.1 appears to allocate memory in 512 byte chunks. But that's as far as I got after 15 minutes of staring at all the underscores and C-style casts tosize_t
. And then I came across this comment:不便于携带。双端队列没有容量成员的原因是它不使用连续内存。从性能角度来看,没有理由考虑它。
Not portably. The reason there's no
capacity
member for deque is because it does not use contiguous memory. There's no reason, performance-wise, to consider it.