复制嵌套向量的最简单方法是什么

发布于 2024-11-26 16:36:59 字数 868 浏览 1 评论 0原文

在我的代码中,我有一个名为 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 技术交流群。

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

发布评论

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

评论(4

世界如花海般美丽 2024-12-03 16:36:59

我想你的意思

boxes = nBoxes;

是最后。

vector 的operator = 复制vector 的内容。也就是说,在您的情况下,它将指针复制到 bead。珠子对象本身不会被深度复制。

I think that you meant

boxes = nBoxes;

in the end.

operator = of vector copies the contents of the vector. That is, in your case it copies the pointers to bead. The bead objects themselves aren't deep-copied.

空宴 2024-12-03 16:36:59

存储在向量 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.

予囚 2024-12-03 16:36:59
nBoxes = boxes;

将制作 boxes 向量的完整副本。对nBoxes 内容所做的任何更改都不会影响boxes 的内容。

但是:如果您对 bead* 指针指向的 bead 对象进行更改,那么这些更改也将在 boxes 向量中看到。
如果你想避免这种情况,你需要制作向量的深层副本。

nBoxes = boxes;

will make a complete copy of the boxes vector. Any changes made to the contents of nBoxes will not impact the content of boxes.

However : if you make changes to the bead objects pointed to by the bead* pointers, then those changes will also be seen in the boxes vector.
If you want to avoid that, you'll need to make a deep copy of the vector.

风铃鹿 2024-12-03 16:36:59

忘记嵌套向量并实现一个自定义类来覆盖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;

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