vectors and the resize function
Hi everyone,
I am having a problem creating vectors of the correct size.
What I am doing is, reading from a file. From this file, the first 2 ints specify rows and columns of a row x col grid. I use vector of vectors to achieve this. Now, I've written this code, and it isn't giving me the desired result. When I cout to the console, it is displaying 5 rows and 40 columns when it should be displaying a 10 x 20 grid. What am I doing wrong here?
Code:
int x;
ifstream input;
input.open( fileName.c_str() );
input >> nRows;
input >> nCols;
cout << "row and columns " << nRows << nCols << endl;
vals.resize(nRows);
markedRecords.resize(nRows);
//cout << "vals size " << vals.size() << endl;
for( int i = 0; i < nRows; i++ )
{
vals[i].resize(nCols);
markedRecords[i].resize(nCols);
for( int j = 0; j < nCols; j++ )
{
input >> x;
vals[i][j] = x;
markedRecords[i][j] = x;
//cout << vals [i][j] << " ";
cout << markedRecords[i][j] << " ";
}
}
input.close();