复制嵌套向量的最简单方法是什么
在我的代码中,我有一个名为 bead
的结构。我有一个向量,定义为
vector<向量<矢量<矢量<珠子*> > > > 。
有时,我需要对 boxes
进行一些更改,并且可能需要恢复这些更改
如果我执行以下操作,会发生什么情况:
vector< vector <vector <vector <bead*> > > > nBoxes;
nBoxes = boxes;
....
//some code that resizes the forth nest in nBoxes and create new content
....
boxes = nBoxes;
框中的数据是否会通过应用于 nBox 的更改而更改?
这是 bead 的定义:
struct bead{
particle mainPart;
int charge;
int type;
double rho;
double nextRho;
int LID;
double U;
double nextU;
bool touch;
};
它需要 article
的定义
struct particle{
vec pos;
vec oldPos;
vec vel;
vec oldVel;
vec F;
vec oldF;
};
vec
是一个类,它保存 velarray 参数以及一堆操作它的函数。
In my code I have a struct named bead
. and I have a vector which is defined as
vector< vector< vector <vector <bead*> > > > boxes
Sometimes, I need to make some changes to the boxes
and I might need to revert those changes.
What happen if I do the following:
vector< vector <vector <vector <bead*> > > > nBoxes;
nBoxes = boxes;
....
//some code that resizes the forth nest in nBoxes and create new content
....
boxes = nBoxes;
Will the data in boxes be changed through changes applied to nBoxes?
here is the definition of bead:
struct bead{
particle mainPart;
int charge;
int type;
double rho;
double nextRho;
int LID;
double U;
double nextU;
bool touch;
};
which requires the definition of particle
struct particle{
vec pos;
vec oldPos;
vec vel;
vec oldVel;
vec F;
vec oldF;
};
vec
is a class that hold a velarray parameters along with bunch of functions to manipulate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想你的意思
是最后。
vector 的
operator =
复制vector
的内容。也就是说,在您的情况下,它将指针复制到bead
。珠子对象本身不会被深度复制。I think that you meant
in the end.
operator =
of vector copies the contents of thevector
. That is, in your case it copies the pointers tobead
. The bead objects themselves aren't deep-copied.存储在向量 of ... 的向量中的数据将正确复制到您的赋值表达式上。
但是向量中存储的数据只是指针!因此,每当您更改一个向量中的珠子数据时,都会在两个向量中更改它,因为两个向量仅包含指向单个内存位置的指针。
The data that is stored in the vector of vector of ... will be copied correctly on your assignement expressions.
BUT the data stored in your vectors is only pointers! Therefore, whenever your changed the
bead
data from one vector, you change it in both, as both vector only contains a pointer to a single memory location.将制作
boxes
向量的完整副本。对nBoxes
内容所做的任何更改都不会影响boxes
的内容。但是:如果您对
bead*
指针指向的bead
对象进行更改,那么这些更改也将在boxes
向量中看到。如果你想避免这种情况,你需要制作向量的深层副本。
will make a complete copy of the
boxes
vector. Any changes made to the contents ofnBoxes
will not impact the content ofboxes
.However : if you make changes to the
bead
objects pointed to by thebead*
pointers, then those changes will also be seen in theboxes
vector.If you want to avoid that, you'll need to make a deep copy of the vector.
忘记嵌套向量并实现一个自定义类来覆盖operator=并按您希望的方式运行(复制子向量)可能是个好主意
第二个想法是使用
vector
向量<向量<向量<珠子> > > >框;
Is probably a good idea to forget about your nested vectors and implement a custom class that overrides operator= and behaves as you wish (copying sub-vectors)
A second idea is to use
vector< vector< vector <vector <bead> > > > boxes;