displaying 2-d vector

Apr 22, 2008 at 7:05am
void display(vector < vector <bool> > g, int rows, int cols)
{
// pre : none
// post : displays the grid

for (int rows = 0; rows < 9; rows++)
{
for (int cols = 0; cols < 30; cols++)
cout << g.at(rows).at(cols)
cout << endl;
}
}

is this right?? if not wat needs changing??
thanks
Apr 22, 2008 at 11:31am
First, your declaration of "rows" and "cols" as for loop indices shadows the parameters of the same name.

Second, I would strongly recommend passing the matrix by const reference to the function, as the overhead of copying the whole thing onto the stack when the function is called is significant.

1
2
3
4
5
void display( vector< vector<bool> > const& g, int rows, int cols ) {
    for( int r = 0; r < rows; ++r )
        for( int c = 0; c < cols; ++c )
            cout << g.at( r ).at( c ) << endl;
}

Topic archived. No new replies allowed.