CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2016
    Posts
    2

    resizing a 2D vector of structs

    Hello, I'm working on making a minesweeper game using C++ but I'm having trouble re sizing a vector.

    Here's what I have:
    A vector of ints:

    Code:
     vector<vector<int> > mineField;
    A vector of structs:

    Code:
    struct cell{
             int value;                //(-1 mine, 0 no surrounding, # > 0 number of surrounding mines)
             int state;		       //( 0 hidden, 1 revealed, 2 marked) 
             bool isMine;
        };
        
        vector<vector<cell> > mineField;
    The vector of cell is located in a separate .cpp file of a minesweeper class.
    What I want to do is re size the vector of cell to have the same dimensions as the vector of ints.
    and initialize the struct variable *value* to the values in the vector of ints and the struct
    variable *state* to 0.

    This is what I have tried so far:

    Code:
    this->mineField.resize(rowNum, vector<cell>(colNum));
            
            for(int i = 0; i < rowNum; i++){
                for(int j = 0; j < colNum; j++){
                    
                    this->mineField[i][j].state = 0;
                        
                    this->mineField[i][j].value = mineField[i][j];
                }
            }
    When attempt to run this I am only able to have the dimensions 5 rows x 5 columns (I cannot figure out why). Any
    other dimensions exits the program and netbeans tells me the run has failed.

    I have also tried:

    Code:
    this->mineField.clear();
            for (int i = 0; i < rowNum; i++){
                
                this->mineField.push_back(vector<cell>(colNum, 0));
            
            }
            
            for(int i = 0; i < rowNum; i++){
                for(int j = 0; j < colNum; j++){
                    
                    this->mineField[i][j].state = 0;
                        
                    this->mineField[i][j].value = mineField[i][j];
                } 
            }
    When trying to resize this way, nothing works.

    And this way:

    Code:
    this->mineField.resize(rowNum);
            
            for(int i = 0; i < rowNum; i++){
                
                this->mineField[i].resize(colNum);
                
            }
            
            for(int i = 0; i < rowNum; i++){
                for(int j = 0; j < colNum; j++){
                    
                    this->mineField[i][j].state = 0;
                        
                    this->mineField[i][j].value = mineField[i][j];
                } 
            }
    Attempting this lets the program run but doesn't work for any dimensions combination.

    Any help is greatly appreciated, thank you.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: resizing a 2D vector of structs

    Code:
    this->mineField[i][j].value = mineField[i][j];
    What is this->mineField[] as opposed to mineField[] ?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: resizing a 2D vector of structs

    Why not simply create a new one and copy the content of old vector to new and then free the old vector . Any internally resize is also going to do this only.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured