是否存在像“std::array”这样的结构,其“使用中”是size 可以小于 max_size 吗?

发布于 2025-01-16 19:54:22 字数 547 浏览 2 评论 0原文

我遇到一个问题,我使用大量双精度向量的加法、减法和乘法 (std:vector)。每个向量可以有 2 到 8 个元素,向量的元素数量可以根据操作而变化,但永远不会超过 8。

执行此操作的最快速有效的方法是使用诸如 之类的类型double my_vector[8]std::array我的向量。但我仍然需要一个单独的变量 int my_vector_size 来跟踪向量实际使用的 8 个预分配插槽中的多少个。 IE。如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文