是否存在像“std::array”这样的结构,其“使用中”是size 可以小于 max_size 吗?
我遇到一个问题,我使用大量双精度向量的加法、减法和乘法 (std:vector
)。每个向量可以有 2 到 8 个元素,向量的元素数量可以根据操作而变化,但永远不会超过 8。
执行此操作的最快速有效的方法是使用诸如 之类的类型double my_vector[8]
或 std::array
my_vector_size = 2
,那么我们将向量视为 {my_vector[0],my_vector[1]}
,其余部分将被忽略,直到某些操作发生变化my_vector_size
。
下一步是将 double my_vector[max_size] 和 int my_vector_size 组合成一个结构。但我想知道 std
是否已经有适合我要求的结构。
I have a problem where I use add, subtract and multiply lots of vectors of doubles (std:vector<double>
). Each vector can have anywhere from 2 to 8 elements, The number of elements of a vector can change depending on the operation, but will never exceed 8.
The most speed efficient way to do this would be to use a types such as double my_vector[8]
or std::array<double, 8> my_vector
. But then I'd still need a separate variable int my_vector_size
to keep track of how many of the 8 preallocated slots the vector is actually using. Ie. if my_vector_size = 2
, then the we consider the vector to be as {my_vector[0],my_vector[1]}
with the rest of them being ignored until some operation changes my_vector_size
.
The next step would be to group double my_vector[max_size]
and int my_vector_size
into a single structure. But I'm wondering if std
already has a structure that fits my requirements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论