std::vector 元素是否保证是连续的?
我的问题很简单:std::vector
元素是否保证是连续的? 换句话说,我可以将指向 std::vector 第一个元素的指针用作 C 数组吗?
如果我没记错的话,C++ 标准并没有做出这样的保证。 然而,std::vector
的要求是,如果元素不连续,实际上不可能满足这些要求。
有人可以澄清这一点吗?
例子:
std::vector<int> values;
// ... fill up values
if( !values.empty() )
{
int *array = &values[0];
for( int i = 0; i < values.size(); ++i )
{
int v = array[i];
// do something with 'v'
}
}
My question is simple: are std::vector
elements guaranteed to be contiguous? In other words, can I use the pointer to the first element of a std::vector
as a C-array?
If my memory serves me well, the C++ standard did not make such guarantee. However, the std::vector
requirements were such that it was virtually impossible to meet them if the elements were not contiguous.
Can somebody clarify this?
Example:
std::vector<int> values;
// ... fill up values
if( !values.empty() )
{
int *array = &values[0];
for( int i = 0; i < values.size(); ++i )
{
int v = array[i];
// do something with 'v'
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
C++98 标准本身遗漏了这一点,但后来作为 TR 的一部分添加了。 即将推出的 C++0x 标准当然会将此作为要求。
来自 n2798(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.
From n2798 (draft of C++0x):
正如其他答案所指出的,向量的内容保证是连续的(除了 bool 的奇怪之处)。
我想添加的评论是,如果您对向量进行插入或删除,这可能会导致向量重新分配其内存,那么您将导致所有保存的指针和迭代器无效。
As other answers have pointed out, the contents of a vector is guaranteed to be continuous (excepting bool's weirdness).
The comment that I wanted to add, is that if you do an insertion or a deletion on the vector, which could cause the vector to reallocate it's memory, then you will cause all of your saved pointers and iterators to be invalidated.
该标准实际上保证了
向量
在内存中是连续的,并且&a[0]
可以传递给C
函数需要一个数组。此规则的例外是
vector
,它每个bool
仅使用一位,因此尽管它确实具有连续内存,但不能用作bool *
(这被广泛认为是错误的优化和错误)。顺便说一句,你为什么不使用迭代器? 这就是他们的目的。
The standard does in fact guarantee that a
vector
is continuous in memory and that&a[0]
can be passed to aC
function that expects an array.The exception to this rule is
vector<bool>
which only uses one bit perbool
thus although it does have continuous memory it can't be used as abool*
(this is widely considered to be a false optimization and a mistake).BTW, why don't you use iterators? That's what they're for.
正如其他人已经说过的,向量内部使用连续的对象数组。 每当调用任何非常量成员函数 IIRC 时,指向该数组的指针都应被视为无效。
不过,有一个例外!!
vector
有一个专门的实现来节省空间,因此每个 bool 仅使用一位。 底层数组不是连续的 bool 数组,并且vector
上的数组算术不像vector
那样工作。(我想这也可能适用于向量的任何专门化,因为我们总是可以实现一个新的。但是,
std::vector
是唯一的、错误的、标准的专门化简单的指针算术将无法工作。)As other's have already said,
vector
internally uses a contiguous array of objects. Pointers into that array should be treated as invalid whenever any non-const member function is called IIRC.However, there is an exception!!
vector<bool>
has a specialised implementation designed to save space, so that each bool only uses one bit. The underlying array is not a contiguous array of bool and array arithmetic onvector<bool>
doesn't work likevector<T>
would.(I suppose it's also possible that this may be true of any specialisation of vector, since we can always implement a new one. However,
std::vector<bool>
is the only, err, standard specialisation upon which simple pointer arithmetic won't work.)是的,std::vector 的元素保证是连续的。
Yes, the elements of a std::vector are guaranteed to be contiguous.
我发现这个线程是因为我有一个用例,其中使用连续内存的向量是一个优势。
我正在学习如何在 OpenGL 中使用顶点缓冲区对象。 我创建了一个包装类来包含缓冲区逻辑,因此我需要做的就是传递一个浮点数组和一些配置值来创建缓冲区。
我希望能够根据用户输入从函数生成缓冲区,因此长度在编译时未知。 这样做是最简单的解决方案:
现在我可以将向量的浮点数作为数组传递给 OpenGL 的缓冲区相关函数。 这也消除了使用 sizeof 来确定数组长度的需要。
这比分配一个巨大的数组来存储浮点数并希望它足够大,或者使用连续存储创建我自己的动态数组要好得多。
I found this thread because I have a use case where vectors using contiguous memory is an advantage.
I am learning how to use vertex buffer objects in OpenGL. I created a wrapper class to contain the buffer logic, so all I need to do is pass an array of floats and a few config values to create the buffer.
I want to be able to generate a buffer from a function based on user input, so the length is not known at compile time. Doing something like this would be the easiest solution:
Now I can pass the vector's floats as an array to OpenGL's buffer-related functions. This also removes the need for sizeof to determine the length of the array.
This is far better than allocating a huge array to store the floats and hoping I made it big enough, or making my own dynamic array with contiguous storage.
cplusplus.com:
cplusplus.com: