问题描述
我写C ++中的MPC控制器,其中包括一个Matrix类,在那里我存储在阵列中的数据,我用c记忆功能(memcpy的,memset的等)。今天我换成C ++矢量阵列和我使用的复印移动存储等...我面临一个问题,用矢量替换阵列几乎使用矢量一倍控制信号的计算时间。
I wrote a MPC controller in C++, which included a Matrix class, where I stored the data in an array and I used C memory functions(memcpy, memset etc). Today I replaced the array with c++ vector and I used copy to move the memory etc... I faced one problem, by replacing the array with vector the calculation time of the control signal almost doubled by using vector.
时有什么缺点,如果我使用的alloc,memcpy的,memset的,免费插件C ++ code?如果有任何那些是什么?
Is there any disadvantages if I'm using alloc,memcpy,memset, free ins c++ code? If there is any what are those?
推荐答案
矢量
中增加了两个方面的价值:
Vector
adds value in 2 aspects:
- 提供额外的功能,这是不是在
çpresent
阵列,如调整大小,检查当前的大小等,你可能会发现从它的接口,更好的选择你的定制的解决方案。 - 渔获一些bug,如解决地址超出
矢量
的范围。
- Provides additional functionality which is not present in
C
array, such as resizing, checking for current size etc. You may find better alternatives from its interface to your custom solution. - Catches some bugs, such as addressing an address beyond the scope of the
vector
.
如果您满意您的专有数据管理和自信在code的质量,你不需要矢量
。但是请注意,这可能会造成一些软件问题,如的C可维护性减少
阵列相比矢量
(例如:未来code可访问一个出界的价值,即使你目前的code不)。
If you are satisfied with your proprietary data management and confident in quality of your code, you don't need vector
. Note, though, this might pose some software issues, such as decreased maintainability of C
array compared to vector
(e.g., future code may access an out of bounds value, even if your current code doesn't).
修改:
见@Als回答针对你的情况可能的替代(的std ::阵列
)。
这篇关于是否有任何缺点,如果我使用数组,而不是矢量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!