简体   繁体   English

C ++:使用std :: vector就像数组一样安全吗?

[英]C++: is it safe to work with std::vectors as if they were arrays?

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. 诸如glVertexPointer函数,需要知道顶点在哪里,它们彼此之间的距离如何等等。 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 of Element *x will be x - & array[0] ) -- btw, this sounds dirty to me: is it good practice or should I do something else? 为了获得该数组中元素的索引,我宁愿避免在元素中包含index字段,而宁愿使用指针算术(即: Element *x索引将为x - & array[0] ) -顺便说一句,对我来说这听起来很肮脏:这是一种好的做法还是我应该做些其他的事情?


Is it safe to use std::vector for this? 为此使用std::vector是否安全?

Something makes me think that an std::array would be more appropriate but: 有些事情使我认为std::array更合适,但:

  • 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 an std::array , thus won't take any overhead due to sporadic reallocation. 我将把std::vector容量设置为我需要的大小(用于std::array的大小,因此由于零星的重新分配而不会产生任何开销。

  • I don't mind a little space overhead for std::vector 's internal structure. 我不介意std::vector的内部结构有一些空间开销。

  • 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). 我可以使用调整向量大小的功能(或更优:在安装过程中选择大小),而且我认为无法使用std :: array来做到这一点,因为它的大小是模板参数(太糟糕了:我即使使用旧的类似C的数组,也可以做到这一点,只需在堆上动态分配它)。


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): 如果std::vector我的需求,那么我想详细了解一下std::array (或纯C数组)是否会有运行时开销:

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? 还要别的吗?

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. 与c样式的数组相比,性能可能受到较小的影响,例如,由于向量实现完成了索引验证。 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. 正如其他人所指出的那样,如果使用指向元素(或迭代器)的指针来访问向量,请确保在使用向量时不要重新分配向量。

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]. 可以将std :: vector中的数据视为数组,并使用&v [0]获取指向其开头的指针。 Obviously if you do anything that can reallocate the data then then you pointers will probably be invalidated. 显然,如果您执行任何可以重新分配数据的操作,则指针可能会失效。

Yep, You can use it as Array in OpenGL :) Example: 是的,您可以在OpenGL中将其用作数组:)示例:

glBufferData( GL_ARRAY_BUFFER_ARB, dataVec.size() * sizeof( dataVec[0] ), &dataVec[0], GL_STATIC_DRAW_ARB );

Where dataVec is std::Vector dataVec是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)? 您的阵列可以变成多大(固定大小,但是在以后的版本中可以增加大小)?

If you really want a std::array you can use boost::array. 如果您真的想要std :: array,可以使用boost :: array。 It is like a common array, but support iterators and you can easily use it with STL algorithms. 它就像一个通用数组,但是支持迭代器,您可以轻松地将其与STL算法一起使用。

在多线程环境中工作和动态内存分配可能会导致问题,因为向量通常是连续的内存块,而指针可能不是连续的块!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM