C ++:是它的安全与性病::向量的工作,如果他们的阵列? [英] C++: is it safe to work with std::vectors as if they were arrays?

查看:83
本文介绍了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:


  • glVertexPointer ,即需要知道的顶点,他们是多么遥远从另一个等功能。在我的情况的顶点将是元素店的会员。

  • 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.

要得到这个数组中的元素的索引,我会preFER,以避免我的元素中的首页字段,但宁愿使用指针算术玩(即:的指数元素* X X - &安培;数组[0] ) - ? - 顺便说一句,这听起来肮脏的对我说:这是很好的做法,或者我应该做些别的事情

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?

时的安全使用的std ::矢量此?

东西让我觉得,一个的std ::阵列会更合适,但是:

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.

我要设置的std ::矢量的容量大小,我需要(这将使用一个 STD大小::阵列,因此不会采取任何开销,由于零星的重新分配。

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 ::矢量的内部结构一点空间开销。

I don't mind a little space overhead for std::vector's internal structure.

我可以用它来调整向量的能力(或更好:有安装过程中选择一个大小),我觉得没有办法用的std ::阵列要做到这一点,因为它的大小是一个模板参数(这太糟糕了:我能做到这一点,即使旧的类C数组,只是对动态堆中分配吧)

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 ::矢量是罚款,我的目的,我想知道到详细信息,如果将对于一些运行时的开销的std ::阵列(或一个普通的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):

我知道,它会叫一次我增加其大小的任何元素的默认构造函数(但我想,如果我的数据已经有一个空的默认构造函数?这不会花费任何东西),同样的析构函数。还有别的吗?

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?

推荐答案

向量都保证有contigous内存中的所有元素,因此它是安全的在你的场景中使用。可以有一个小的性能损失相对于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.

这篇关于C ++:是它的安全与性病::向量的工作,如果他们的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆