在c++中移动语义2d向量



我有一个关于二维矢量(或矢量的矢量)的c++移动语义的问题。它来源于一个动态规划问题。为简单起见,我只举一个简化版的例子。

//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);
}

那么,是否可以简单地使用' result = move(tmp);'对于2D矢量的移动语义?

是的,因为结果不是'2D'向量,它只是向量的1-D向量。

最新更新