C++ copying multidimensional vector

16,673

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

Solution 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.

Solution 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];
Share:
16,673
sleepless_in_seattle
Author by

sleepless_in_seattle

Updated on June 04, 2022

Comments

  • sleepless_in_seattle
    sleepless_in_seattle about 2 years

    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?

  • sleepless_in_seattle
    sleepless_in_seattle over 11 years
    I tried it but it doesn't work, it just gives me random numbers. vector < vector < int > > a; a.resize(1); a[0][0] = 123; vector < vector < int > > b = a; cout << b[0][0];
  • Dan
    Dan over 11 years
    I've added why that happens to my answer.
  • sleepless_in_seattle
    sleepless_in_seattle over 11 years
    You seem to be correct. I transformed both of them to one dimensional arrays and it worked. I'm a bit confused here. Am I understanding the resize wrong? I thought it was making me a 10 rows with N columns since I was able to do a[0][100] = 123; and a[9][10000] = 123; I see Yakk's already explained this.
  • Joseph Mansfield
    Joseph Mansfield over 11 years
    a is a vector. Resizing a vector gives it N default initialized elements. Each element of a is a vector and when they are default initialized they are empty. So resizing a gives you 10 empty vectors.