jaedong
jaedong

Reputation: 47

move semantics 2d vector in C++

I have a question on C++ move semantics in 2D vector (or a vector of vectors). It comes from a problem of dynamic programing. For simplicity, I just take a simplified version as the example.

//suppose I need to maintain a 2D vector of int with size 5 for the result. 
vector<vector<int>> result = vector<vector<int>>(5);

for(int i = 0; i < 10; i++){
  vector<vector<int>> tmp = vector<vector<int>>(5);
  
  //Make some updates on tmp with the help of result 2D vector
  
  /*
    Do something
  */

  //At the end of this iteration, I would like to assign the result by tmp to prepare for next iteration.

  // 1) The first choice is to make a copy assignment, but it might introduce some unnecessary copy
  // result = tmp;

  // or

  // 2) The second choice is to use move semantics, but I not sure if it is correct on a 2D vector. 
  // I am sure it should be OK if both tmp the result are simply vector (1D).
  // result = move(tmp);

}



So, is it OK to simply use `result = move(tmp);' for the move semantics of 2D vector?

Upvotes: 0

Views: 522

Answers (1)

wreathbro
wreathbro

Reputation: 61

Yes, because result is not '2D' vector, it's simply 1-D vector of vectors.

Upvotes: 1

Related Questions