C++:像使用数组一样使用 std::vector 是否安全?
我需要有一个固定大小的元素数组,并调用它们需要知道它们如何放置在内存中的函数,特别是:
像
glVertexPointer
这样的函数,需要知道顶点在哪里,它们彼此之间有多远等等。在我的例子中,顶点将是要存储的元素的成员。要获取此数组中元素的索引,我宁愿避免在元素中包含
index
字段,而是宁愿使用指针算术(即:的索引)元素 *x
将是x - & array[0]
) -- 顺便说一句,这对我来说听起来很肮脏:这是好的做法还是我应该做点别的事情?
为此使用 std::vector 是否安全?
有些事情让我认为 std::array 会更合适,但是:
我的结构的构造函数和析构函数很少被调用:我不介意这样的开销。
我将把
std::vector
容量设置为我需要的大小(用于std::array
的大小,因此不会由于零星的重新分配而产生任何开销。我不介意
std::vector
的内部结构有一点空间开销。我可以使用调整向量大小的功能(或者更好:在设置过程中选择大小)。 ,而且我认为没有办法用 std::array 来做到这一点,因为它的大小是一个模板参数(这太糟糕了:即使使用旧的类似 C 的数组,我也可以做到这一点,只需在堆上动态分配它)。
如果 std::vector
适合我的目的,我想详细了解它是否会相对于 std::array
(或对于普通 C 数组):
我知道一旦我增加任何元素的大小,它就会调用默认构造函数(但我想如果我的数据有一个空的默认构造函数,这不会花费任何成本?),析构函数也是如此。还要别的吗?
I need to have a fixed-size array of elements and to call on them functions that require to know about how they're placed in memory, in particular:
functions like
glVertexPointer
, that needs to know where the vertices are, how distant they are one from the other and so on. In my case vertices would be members of the elements to store.to get the index of an element within this array, I'd prefer to avoid having an
index
field within my elements, but would rather play with pointers arithmetic (ie: index ofElement *x
will bex - & array[0]
) -- btw, this sounds dirty to me: is it good practice or should I do something else?
Is it safe to use std::vector
for this?
Something makes me think that an std::array
would be more appropriate but:
Constructor and destructor for my structure will be rarely called: I don't mind about such overhead.
I'm going to set the
std::vector
capacity to size I need (the size that would use for anstd::array
, thus won't take any overhead due to sporadic reallocation.I don't mind a little space overhead for
std::vector
's internal structure.I could use the ability to resize the vector (or better: to have a size chosen during setup), and I think there's no way to do this with std::array, since its size is a template parameter (that's too bad: I could do that even with an old C-like array, just dynamically allocating it on the heap).
If std::vector
is fine for my purpose I'd like to know into details if it will have some runtime overhead with respect to std::array
(or to a plain C array):
I know that it'll call the default constructor for any element once I increase its size (but I guess this won't cost anything if my data has got an empty default constructor?), same for destructor. Anything else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
保证向量的所有元素都位于连续内存中,因此在您的场景中使用它是安全的。与 c 样式数组相比,性能可能会受到较小的影响,例如由于向量实现完成的索引验证。在大多数情况下,性能是由其他因素决定的,所以我不会担心这一点,直到性能的实际测量表明这是一个真正的问题。
正如其他人所指出的,如果您使用指向元素(或迭代器)的指针来访问向量,请确保在使用向量中存储的数据时不要重新分配该向量。
Vectors are guaranteed to have all elements in contigous memory, so it is safe to use in your scenario. There can be a small performance hit compared to c-style arrays, for instance due to index validations done by the vector implementation. In most cases, the performance is determined by something else though, so I wouldn't worry about that until actual measurements of performance show that this a real problem.
As pointed out by others, make sure that you don't reallocate the vector while you are making use of the data stored in it if you are using pointers to elements (or iterators) to access it.
可以将 std::vector 中的数据视为数组,使用 &v[0] 获取指向其开头的指针。显然,如果您执行任何可以重新分配数据的操作,那么您的指针可能会失效。
It's fine to treat the data in a std::vector as an array, get a pointer to the start of it with &v[0]. Obviously if you do anything that can reallocate the data then then you pointers will probably be invalidated.
是的,您可以在 OpenGL 中将其用作数组:) 示例:
其中 dataVec 是 std::Vector
Yep, You can use it as Array in OpenGL :) Example:
Where dataVec is std::Vector
它甚至比在堆栈上放置数组更安全:您的堆栈到底有多大?你的数组可以有多大(固定大小,但在以后的版本中可以增加大小)?
It is even safer than having an array on the stack: how big really is your stack? how big could your array become (fixed size, but the size could be increased in later versions)?
如果你真的想要一个 std::array ,你可以使用 boost::array 。它就像一个普通的数组,但支持迭代器,您可以轻松地将它与STL算法一起使用。
If you really want a std::array you can use boost::array. It is like a common array, but support iterators and you can easily use it with STL algorithms.
在多线程环境和动态内存分配中工作可能会导致问题,因为向量通常是连续的内存块,而指针可能不是!
Working in multithreading environment and dynamic memory allocation might cause problem because vector is usually a continuous chunk of memory and of pointers might not!