C++ copying multidimensional vector

回眸只為那壹抹淺笑 提交于 2020-01-05 07:52:11

问题


I'm having problems copying a multidimensional vector, I've tried many things but this is the last one:

vector < vector < int > > a;
vector < vector < int > > b;
a.resize(10);
b.resize(10);
a[0][0] = 123;
copy( a.begin(), a.end(), back_inserter(b) );
cout << b[0][0];

I'm trying to do a recursive loop that counts all possible routes in a grid within 10 moves. I'm trying to create a vector called current_path which would hold the current path for each recursion, when the current_path has 10 moves, It will copy the data from current_path to all_paths.

The grid goes like this:

0  1  2 3
4  5  6 7
8  9  10 11
12 13 14 15

You can only move to a square you touch so from 0 you can move to 1, 4 and 5. And from 1 to 3, 4, 5, 6 etc.

The main idea is to copy the current_path to the next function call (recursive) so it would hold the curren_path up to that point, doing that until it's full (10 steps). After it's copied from current_path to all_paths I suppose I have to delete the current_path?

I know how to efficiently calculate all steps but I'm having trouble copying the current_path and propably and how do I add the current_path to all_paths when I'm at 10 steps?


回答1:


There are a few problems with your code. By the end of line 4, you have two vectors that each contain 10 empty vectors. You could visualize it like this:

a = {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
b = {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}

Those inner vectors still do not have any elements so when you try and set a[0][0] to 123, you're accessing an element that doesn't exist, invoking undefined behaviour.

If that had worked, your use of std::copy would simply copy each of the vectors from a and pushed it to the back of b. Since b already has 10 elements, it would now have 20 elements.

Then you try to output b[0][0] which doesn't exist just as much as a[0][0] didn't either.

The solution here is to simply use the copy assignment operator defined by std::vector:

vector<vector<int>> a = {{1, 2, 3}, {4, 5}};
vector<vector<int>> b;
b = a;



回答2:


You can just do b = a;

std::vector defines a copy assignment operator which does an elementwise copy. This will invoke the copy assignment operator of the inner vector, which copies the ints.

Instead of

a.resize(10);
a[0][0] = 123;

You will want to do

a.resize(10);
a[0].push_back(123);

because while resize creates 10 new vectors in the outer vector, these inner vectors have length 0, so doing a[0][0] will give you an element one past the end of the first inner vector.

Also, as long as you create the vectors on the stack (as you have done) you will not need to delete anything; they have automatic storage duration.




回答3:


Here is the fixed version of your code:

vector < vector < int > > a;
vector < vector < int > > b;
a.resize(10, vector < int >(10));
b.resize(10, vector < int >(10));
a[0][0] = 123;
b = a;
cout << b[0][0];


来源:https://stackoverflow.com/questions/13571565/c-copying-multidimensional-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!