且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

std::vector 元素是否保证是连续的?

更新时间:2023-11-12 08:00:58

这在 C++98 标准中被遗漏,但后来作为 TR 的一部分添加.即将推出的 C++0x 标准当然会将此作为要求包含在内.

This was missed from C++98 standard proper but later added as part of a TR. The forthcoming C++0x standard will of course contain this as a requirement.

来自 n2798(C++0x 草案):

From n2798 (draft of C++0x):

23.2.6 类模板向量[向量]

1 向量是支持随机访问迭代器的序列容器.此外,它支持(摊销)最后的恒定时间插入和擦除操作;在中间插入和擦除需要线性时间.贮存管理是自动处理的,但可以提供提示以提高效率.的元素向量是连续存储的,这意味着如果 v 是一个向量,其中 T 是其他类型比 bool,那么它遵守恒等式 &v[n] == &v[0] + n 对于所有 0 <= n <v.size().

1 A vector is a sequence container that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency. The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().