简体   繁体   中英

Inserting values to a multidimensional-vector in C++

I've got a minor problem.

I'm using multidimensional-vectors and I want to insert some values to it at a given position. I'm making a sudoku in wxWidgets and i'm getting the tiles the player have put in and wanting to store them in my mVector.

The mVector looks like this.

vector< vector<string> > board{9, vector<string>(9)};

And at first i've added values just like this.

board[row][col] = value;

"value" is a string and row/col are ints.

Is this a legit way of adding values to the mVector? I'm asking this because when I update the board, by doing this above, I for some reason can't run my other functions where i'm solving the board, giving a hint to the board and so on. Before i store the new values to it all the functions works correkt. Do I maby need to use some other type of build in functions for the vector like insert, push_back or something instead?

Since you declared the vector as size 9x9, yes that is a valid way of assigning values.

Otherwise you could declare the board as

vector<vector<string>> board;

Then fill it with

for (int i = 0; i < 9; ++i)
{
    vector<string> row;
    for (int j = 0; j < 9; ++j)
    {
        row.push_back(value);  // where value is whatever you want
    }
    board.push_back(row);
}

But again, once the board is of size 9x9, you can simply assign a value at any cell for example

board[2][4] = "hello";

Working example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM