[Solved] Vector<Vector<int> > fills with same data

Hey guys and girls,

Sorry if this has been asked a thousand times before but I've searched and not found an answer so thought I'd ask in case others are having the same problem (but mostly cause I am and I can't figure it out!)

I want to return a 2D int array from a function, and that's my first mistake! I see C doesn't like this so I've changed it to a Vector<Vector<int> > but being a stubborn Java-lover I'm still manipulating it like a 2D array. As far as I am aware this is acceptable and even appears to be working except that the values in each 1st dimension are just repeated over and over (I'm considering the 1st dimension to be the left one in the [][] pair).

In case my explanation doesn't make sense here is a little code and the resultant output:

1
2
3
4
5
6
7
8
9
10
11
12
13
Vector<Vector<int> > gameState (10, Vector<int> (10));
for (int row = 0; row < 10; row++) {
    for (int column = 0; column < 10; column++) {
        gameState[row][column] = (column * 10) + row;
    }
}
cout << "Game state:\n";
for (int x = 0; x < 10; x++) {
    for (int y = 0; y < 10; y++) {
       cout << gameState[x][y] << " ";
    }
    cout << "\n";
}


And the output is.... well, you tell me ;) From my reckoning it should be
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
... ...
but it's not.

Ok, here is the actual output:
Game state:
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99
9 19 29 39 49 59 69 79 89 99

Strange, no?
Last edited on
I copy and pasted your code and got
0 10 20 ....
1
2
3

Which is the expected output.
Change

1
2
3
4
5
for (int row = 0; row < 10; row++) {
    for (int column = 0; column < 10; column++) {
        gameState[row][column] = (column * 10) + row;
    }
}


to

1
2
3
4
5
for (int row = 0; row < 10; row++) {
    for (int column = 0; column < 10; column++) {
        gameState[row][column] = (row * 10) + column;
    }
}


Also instead of

Vector<Vector<int> > gameState (10, Vector<int> (10));
it will be simpler to write

Vector<Vector<int> > gameState (10, 10 );
Last edited on
Wow cheers for the quick reply guys!

vlad - I'll give it a try and let you know, thanks.

clanmjc - Hmm that is strange. I'm actually calling it as a function but I didn't think that would make a difference so shortened it for brevity. Here is the full code if you would like to try for your own curiosity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Function prototype
Vector<Vector<int> > getGameState(Vector<Mat> gamePieceImages);

//Call to function
Vector<Vector<int> > gameState = getGameState(gamePieces);


//Function definition
Vector<Vector<int> > getGameState(Vector<Mat> gamePieceImages) {
   Vector<Vector<int> > gameState (10, Vector<int> (10));
   for (int row = 0; row < 10; row++) {
        for (int column = 0; column < 10; column++) {
            gameState[row][column] = (column * 10) + row;
         }
    }
   cout << "Game state:\n";
        for (int x = 0; x < 10; x++) {
            for (int y = 0; y < 10; y++) {
                cout << gameState[x][y] << " ";
            }
            cout << "\n";
        }
    return gameState;


I know I don't use the parameter in the function, I've removed the manipulation on it for now until I get this figured out.
Do you need the parameter gamePieceImages? If you do, pass it by const reference, if you need it to change, pass it by reference.

What is this? Is this actually in your code, if so, remove it. (Well remove it if you have it in your code like it is, if it's in main somewhere and you are just consolidating your code for this post, disregard)
1
2
//Call to function
Vector<Vector<int> > gameState = getGameState(gamePieces);
Last edited on
Haven't had a chance to try suggestions yet but will do over the weekend and report back.

clanmjc - Yes that is in my main function and I'm consolidating my code. When I said this is my full code I meant my code related to the function. I do need the parameter as I do some manipulation on it and set the values of gameState based on it but for the sake of the example I've removed my manipulation of it and just put in the loop that gives the posted results.
Tried swapping round column and row as per your suggestion vlad but the output was still incorrect, all be it with different values repeated on each line.

I've made the changes you suggested as well clanmjc but again they haven't altered my output.

I added in another cout when I'm allocating the values to make sure they are going in right. They are, so it's either afterwards the values are being changed or I'm not referencing the right place.

I noticed as well I'm using Vector with a capital V and not a small v as in vector but really it shouldn't matter since Vector is pretty much the same thing AFAIK in the library I'm using.
Ok I've got it figured out.

The main confusion came from the fact that the library I am using, the OpenCV library, has a class called Vector with an upper case V. And it would appear that C has one called vector with a lower class v.

To those who don't know better, like myself, they are easily confused as capital V Vector appears to work just as you would expect small v vector to. Clearly by the output I'm getting this isn't the case.

I also couldn't get small case vector to work as although I new it was in the std namespace I didn't realise I had to import it using #include <vector>.

Soon as I got that sorted I swapped all my big V Vectors for little v vectors and all is working as expected.

Cheers for the help guys!
Topic archived. No new replies allowed.