Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to copy a nested vector

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.

like image 356
Yotam Avatar asked Oct 22 '25 05:10

Yotam


1 Answers

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.

like image 81
Armen Tsirunyan Avatar answered Oct 23 '25 21:10

Armen Tsirunyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!