Thread: vectors and the resize function

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    14

    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();

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You should be couting a newline between lines 30 and 31.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    14

    recursive use

    That did the trick exactly. Thanks.

    Now that I figured that wasn't my problem, I've come to it and it has to do with this recursive function I wrote. Imagine two grids, filled with 0s and 1s, and these two grids are identical in content, but not addresses. I iterate through each position in the grid and compare if they are the same. If they are the same, then I call the recursive function. This function then changes the value from 1 to -1 to indicate it has been there. It then checks to see if it can go NSEW (north south east west) by seeing if there are other 1's in those positions, and marks them. I am getting an error when traversing through this grid. I think it has something to do with my bounds checking.

    Code:
     
    void Grid::markRegions(  int r,  int c ){
        if( markedRecords[r][c] != -1 ) //if cell is not marked
        {
            mark(r, c);
            if( c+1 < vals[c].size() && vals[r][c+1] == 1 ) //go east
            {
                markRegions(r, c+1);
            }
            if( r+1 < vals[r].size() && vals[r+1][c] == 1 ) //go south
            {
                markRegions(r+1, c);
            }
            if( r-1 != -1 && vals[r-1][c] == 1 ) //go north
            {
                markRegions(r-1, c);
            }
            if( c-1 != -1 && vals[r][c-1] == 1 ) //go west
            {
                markRegions(r, c-1);
            }
    
    
        } //end of top if
    }
    Code:
    void Grid::mark(int r, int c)
    {
        markedRecords[r][c] = -1;
    }
    Code:
    void Grid::traverse()
    {
        for( int i = 0; i < nRows; i++ )
        {
            for( int j = 0; j < nCols; j++ )
            {        
                if( vals[i][j] == 1 && markedRecords[i][j] == 1 )
                {
                    markRegions(i, j);
                }
            }
        }
    }
    My traverse function calls the recursive function. I have posted the code so you know exactly what each function does instead of "guessing".

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Should these two
    Code:
    if( c+1 < vals[c].size() && vals[r][c+1] == 1 ) //go east 
    ...
    if( r+1 < vals[r].size() && vals[r+1][c] == 1 ) //go south
    be like this?
    Code:
    if( c+1 < vals[r].size() && vals[r][c+1] == 1 ) //go east 
    ...
    if( r+1 < vals.size() && vals[r+1][c] == 1 ) //go south
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    14
    *facepalm*

    I owe you. I now remember why vectors yell at you when you try to treat them like arrays. oogabooga thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting function to include vectors
    By a.mlw.walker in forum C Programming
    Replies: 17
    Last Post: 08-10-2011, 08:33 AM
  2. problems with stl vectors function push_back
    By _lazycat_ in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2009, 02:53 PM
  3. Disableing the "resize" function on a window
    By Queatrix in forum Windows Programming
    Replies: 15
    Last Post: 04-24-2005, 01:53 PM
  4. Vectors of pointers and function templates
    By 7words in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2004, 11:39 AM
  5. Function to resize child windows?
    By Clyde in forum Windows Programming
    Replies: 4
    Last Post: 05-28-2002, 08:33 PM