我想与一个或多个 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.
发布评论
评论(4)
不,你不能这样做。标准库容器始终管理自己的内存。
最好的选择是创建
std::vector
,然后在需要时将其用作数组(通过&v[0]
,假设向量非空)。如果您只想拥有容器接口,请考虑使用
std::array
(或boost::array
或std::tr1::array
>) 或者编写自己的容器接口来封装数组。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
(orboost::array
orstd::tr1::array
) or writing your own container interface to encapsulate the array.在我看来,这听起来就像您想用向量为数组别名。因此从逻辑上讲,您需要一个引用向量(由于语法原因,这不起作用)。如果您确实需要此功能,您可以编写自己的
ref
包装类,其行为与实际的 C++ 引用完全相同,因此vn< 的用户/code> 向量无法区分
vector
和vector 。 >
(例如使用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 yourvn
vectors wont be able to distinguish betweenvector<T>
andvector<ref<T> >
(e.g. withT = 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 :)
好的,标准库容器既是信息的持有者,又是这些元素的枚举器。也就是说,几乎任何容器都可以在几乎任何算法中使用,至少,您可以使用
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()
andend()
.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.为了回答你的问题,据我所知,不能给 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.