Hi
The following program was my attempt to implement 2D vectors:
The for loop has been commented on purpose. If it is uncommented, the output would be as expected...Code:#include<cstdio> #include<vector> int main() { int size,counter=0; printf("Enter size:\n"); scanf("%d",&size); std::vector< std::vector<int> > testtwo(size); for(int i=0;i<size;i++) { //for(int j=0;j<size;j++) testtwo[i].push_back(0); } for(int i=0;i<size;i++) { for(int j=0;j<size;j++) { testtwo[i][j]=(counter++); } } for(int i=0;i<size;i++) { for(int j=0;j<size;j++) { printf("%4d",testtwo[i][j]); } printf("\n\n"); } }
0 1 2
3 4 5
6 7 8
But if the for loop is commented, the output is changed to:
0 1 4 5
4 5 8 9
8 9 12 13
12 13 14 15
Please notice that testtwo[row][col] = testtwo[row-1][col +2]. This pattern is followed throughout the output,,,
Could someone please explain this error?
Thanx in advance...



LinkBack URL
About LinkBacks


