与 STL 向量共享数组

发布于 2024-12-04 05:26:34 字数 452 浏览 0 评论 0 原文

我想与一个或多个 STL 向量 v1 共享大小为 k 的双精度数组 a 的内容>,v2...vn

我希望从这个共享存储中获得的效果是,如果底层数组被修改,则可以从与该数组共享其内容的所有向量中观察到更改。

我可以通过将向量 v1...vn 定义为指针向量

vector<double*> v1;

并将指针 a 复制到 a + k 来做到这一点 到这个向量中。但是,我不喜欢这个解决方案。我希望向量是双精度向量。

鉴于您可以从向量中提取底层指针,我假设可以用数组初始化向量,从而共享内容。非常感谢有关如何执行此操作的帮助。

I would like to share the contents of an array of doubles a of size k with one or more STL vectors v1, v2...vn.

The effect that I want from this shared storage is that if the underlying array gets modified the change can be observed from all the vectors that share its contents with the array.

I can do that by defining the vectors v1...vn as vectors of pointers

vector<double*> v1;

and copy the pointers a to a + k into this vector. However, I do not like that solution. I want the vectors to be a vector of doubles.

Given that you can extract the underlying pointer from a vector I am assuming one could initialize a vector with an array in such a way that the contents are shared. Would appreciate help about how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

洒一地阳光 2024-12-11 05:26:34

鉴于您可以从向量中提取底层指针,我假设可以用数组初始化向量,从而共享内容。

不,你不能这样做。标准库容器始终管理自己的内存。

最好的选择是创建 std::vector ,然后在需要时将其用作数组(通过 &v[0],假设向量非空)。

如果您只想拥有容器接口,请考虑使用 std::array (或 boost::arraystd::tr1::array >) 或者编写自己的容器接口来封装数组。

Given that you can extract the underlying pointer from a vector I am assuming one could initialize a vector with an array in such a way that the contents are shared.

No, you can't do this. The Standard Library containers always manage their own memory.

Your best option is to create the std::vector<double> and then use it as an array where you need to do so (via &v[0], assuming the vector is non-empty).

If you just want to have the container interface, consider using std::array (or boost::array or std::tr1::array) or writing your own container interface to encapsulate the array.

虫児飞 2024-12-11 05:26:34

在我看来,这听起来就像您想用向量为数组别名。因此从逻辑上讲,您需要一个引用向量(由于语法原因,这不起作用)。如果您确实需要此功能,您可以编写自己的 ref 包装类,其行为与实际的 C++ 引用完全相同,因此 vn< 的用户/code> 向量无法区分 vectorvector 。 >(例如使用T = double)。但在内部,您可以将向量中的项目链接到“主”数组中的项目。

但你应该有充分的理由来做这个空中马戏团:)

This sounds to me like you want to alias the array with a vector. So logically you want a vector of references (which doesn't work for syntactical reasons). If you really really need this feature, you can write your own ref wrapper class, that behaves exactly like an actual C++ reference, so the users of your vn vectors wont be able to distinguish between vector<T> and vector<ref<T> > (e.g. with T = double). But internally, you could link the items in the vectors to the items in your "master" array.

But you should have darned good reasons to do this overhead circus :)

千秋岁 2024-12-11 05:26:34

好的,标准库容器既是信息的持有者,又是这些元素的枚举器。也就是说,几乎任何容器都可以在几乎任何算法中使用,至少,您可以使用 begin()end() 来遍历它们。

当您将两者(元素保存和元素枚举)分开时,如您的情况,您可以考虑 boost.range。 boost.range 为您提供一对迭代器,用于界定算法的应用范围,并且您的数组中拥有实际的内存存储。这主要适用于读取访问它们,因为通常修改向量的结构将使迭代器无效。不过,您可以重新创建它们。

OK, Standard Library containers are both holders of information, and enumerators for those elements. That is, roughly any container can be used in almost any algorithm, and at least, you can go through them using begin() and end().

When you separate both (element holding and element enumeration), as in your case, you may consider boost.range. boost.range gives you a pair of iterators that delimit the extent to which algorithms will be applied, and you have the actual memory store in your array. This works mostly to read-access them, because normally, modifying the structure of the vector will invalidate the iterators. You can recreate them, though.

荒人说梦 2024-12-11 05:26:34

为了回答你的问题,据我所知,不能给 std::vector 提供一个已经构造的数组来使用。我什至无法想象如何做到这一点,因为还有与尺寸/容量相关的变量。您可以尝试使用自定义分配器来实现这一点,但我觉得它会很难看,容易出错,而且对于将来的维护来说不直观。

也就是说,如果我可以稍微改一下你的话,你是在要求对同一个 std::vector 的多个引用。我要么这样做,要么考虑对向量使用shared_ptr。

To answer your question, as far as I know std::vector can not be given an already constructed array to use. I can not even think how that could be done since there are also the size/capacity related variables. You can possibly try to hack a way to do it using a custom allocator but I feel it will be ugly, error prone and not intuitive for future maintenance.

That said, if I may rephrase your words a bit, you are asking for multiple references to the same std::vector. I would either do just that or maybe consider using a shared_ptr to a vector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文