简体   繁体   中英

C++ copying multidimensional vector

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?

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;

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 int s.

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 .

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM