c++ - How can I modify elements inside vectors? -
i'm trying modify elements inside vector. after change block in array , display again, won't show new values instead, retain previous output. doing wrong?
//////////////////////////////////////////////////// // displays array/maze //////////////////////////////////////////////////// void displaymaze( vector< vector<char> > &maze ) { for( int = 0; < row; i++ ) { for( int j = 0; j < col; j++ ) { cout << "[" << maze[ ][ j ] << "] "; } cout << endl; } } //////////////////////////////////////////////////// // change element //////////////////////////////////////////////////// void updatemouse( vector< vector<char> > maze, const int &mouse_row, const int &mouse_col ) { for( int row = 0; row < row; row++ ){ for( int col = 0; col < col; col++ ) { if( ( row == mouse_row ) && ( col == mouse_col ) ) { maze[ row ][ col ] = 'm'; break; } } } }
updatemouse
takes maze
argument by value. changes makes vector
made local copy within function, destroyed when function exits. change function takes maze
argument by reference.
void updatemouse( vector<vector<char>>& maze, const int &mouse_row, const int &mouse_col ) { // ^^^
the updatemouse
function can simplified to
void updatemouse( vector<vector<char>>& maze, int mouse_row, int mouse_col ) { if(mouse_row < maze.size()) { if(mouse_col < maze[mouse_row].size()) { maze[mouse_row][mouse_col] = 'm'; } } }
Comments
Post a Comment