2d Vector with wrong values

Posted by Petris Rodrigo Fernandes on Stack Overflow See other posts from Stack Overflow or by Petris Rodrigo Fernandes
Published on 2012-03-20T23:18:13Z Indexed on 2012/03/20 23:29 UTC
Read the original article Hit count: 205

Filed under:

I'm studing STL, then i thought "i'll make a 2d array!" but whatever...

a coded this:

vector< vector<int> > vetor;

vetor.resize(10);
vetor[0].resize(10);

for(int i = 0; i < vetor.capacity(); i++){
    for(int h = 0; h < vetor[0].capacity();h++){
        vetor[i][h] = h;
    }

}

Until here, ok. But when i try to show the array value a use this:

for(int i = 0; i < vetor.capacity(); i++){
    cout << "LINE " << i << ": ";
    for(int h = 0; h < vetor[0].capacity();h++){
        cout << vetor[i][h] <<" ";
    }
    cout << "\n";
}

And the results are really wrong:

LINE 0: 4 5 6 7 8 9 6 7 8 9
LINE 1: 0 1 2 3 0 1 2 3 0 1
LINE 2: 0 1 2 3 0 1 2 3 0 1
LINE 3: 0 1 2 3 0 1 2 3 0 1
LINE 4: 0 1 2 3 0 1 2 3 0 1
LINE 5: 0 1 2 3 0 1 2 3 0 1
LINE 6: 0 1 2 3 0 1 2 3 0 1
LINE 7: 0 1 2 3 0 1 2 3 0 1
LINE 8: 0 1 2 3 0 1 2 3 4 5
LINE 9: 0 1 2 3 4 5 6 7 8 9

What's happening with my program? it isn't printing the right values!

© Stack Overflow or respective owner

Related posts about c++