在C ++中将值插入到多维向量中 [英] Inserting values to a multidimensional-vector in C++

查看:195
本文介绍了在C ++中将值插入到多维向量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题。

我使用多维向量,我想在给定位置插入一些值。我在wxWidgets中做一个数独,我得到的玩家已经放在瓷砖,并希望将它们存储在我的mVector。

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.

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是字符串,row / col是整数。

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

这是一个向mVector添加值的合法方式吗?我问这个,因为当我更新板,通过这样做,我有某些原因不能运行我的其他功能,我解决板,给董事会提示等。在我存储新值之前,所有的功能工作correkt。

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?

推荐答案

因为我需要使用一些其他类型的内置函数的插入,push_back或者东西你宣布的矢量为大小9x9,是的,这是一种有效的方式分配值。

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;

然后填写

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

但是,一旦棋盘大小为9x9,例如

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

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

工作示例

这篇关于在C ++中将值插入到多维向量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆