如何在C ++中使用嵌套向量? [英] How do I work with nested vectors in C++?
问题描述
我正在尝试为我正在编写的数独难题求解器使用整数向量的向量.
I'm trying to work with vectors of vectors of ints for a sudoku puzzle solver I'm writing.
问题1:
如果我要按索引访问我的2d向量,是否必须先用适当的大小对其进行初始化?
If I'm going to access a my 2d vector by index, do I have to initialize it with the appropriate size first?
例如:
typedef vector<vector<int> > array2d_t;
void readAPuzzle(array2d_t grid)
{
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
cin >> grid[i][j];
return;
}
int main()
{
array2d_t grid;
readAPuzzle(grid);
}
将出现段故障.我认为这是因为它正在尝试访问尚未初始化的网格元素?
Will seg fault. I assume this is because it is trying to access elments of grid that have not yet been initialized?
我已将网格的声明行换成:
I've swapped out grid's declaration line with:
array2d_t grid(9, vector<int>(9, 0));
这似乎摆脱了这个段错误.这是正确的处理方式吗?
And this seems to get rid of this seg fault. Is this the right way to handle it?
问题2:
为什么当我尝试从cin读取网格并打印出网格时,网格为空白?
Why is it that when I try to read into my grid from cin, and then print out the grid, the grid is blank?
我正在使用以下代码来这样做:
I'm using the following code to do so:
void printGrid(array2d_t grid)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cout << grid[i][j] + " ";
}
cout << endl;
}
}
void readAPuzzle(array2d_t grid)
{
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
cin >> grid[i][j];
return;
}
int main()
{
array2d_t grid(9, vector<int>(9, 0));
printGrid(grid);
readAPuzzle(grid);
printGrid(grid);
}
然后我尝试运行程序,如下所示:
And I attempt to run my program like:
./a.out < sudoku-test
sudoku-test是一个包含以下内容的文件:
Where sudoku-test is a file containing the following:
3 0 0 0 0 0 0 0 0
5 8 4 0 0 2 0 3 0
0 6 0 8 3 0 0 7 5
0 4 1 0 0 6 0 0 0
7 9 0 0 2 0 0 5 1
0 0 0 9 0 0 6 8 0
9 3 0 0 1 5 0 4 0
0 2 0 4 0 0 5 1 8
0 0 0 0 0 0 0 0 6
第一次调用printGrid()会给出一个空白网格,相反,我应该看到一个0的9x9网格,因为这是我初始化它的方式.第二个调用应包含上面的网格.但是,两次都为空.
The first call to printGrid() gives a blank grid, when instead I should be seeing a 9x9 grid of 0's since that is how I initialized it. The second call should contain the grid above. However, both times it is blank.
任何人都可以对此有所了解吗?
Can anyone shed some light on this?
推荐答案
Q1:是的,这是处理它的正确方法.但是,请注意,嵌套向量是实现2D数组的一种相当低效的方法.一个向量并按 x + y * width
计算索引通常是一个更好的选择.
Q1: Yes, that is the correct way to handle it. However, notice that nested vectors are a rather inefficient way to implement a 2D array. One vector and calculating indices by x + y * width
is usually a better option.
Q2A:计算 grid [i] [j] +"
不会连接两个字符串(因为左侧是int,而不是字符串),而是将数字值添加到指针(字符串"的第一个字符的内存地址).使用 cout<<grid [i] [j]<"
代替.
Q2A: Calculating grid[i][j] + " "
does not concatenate two strings (because the left hand side is int, not a string) but instead adds the numeric value to a pointer (the memory address of the first character of the string " "). Use cout << grid[i][j] << " "
instead.
Q2B:您正在按值传递数组(它被复制)用于 readAPuzzle
.该函数读入其本地副本,该本地副本在函数返回时被销毁.而是通过引用传递(这样避免了复制,而改用原始的):
Q2B: You are passing the array by value (it gets copied) for readAPuzzle
. The the function reads into its local copy, which gets destroyed when the function returns. Pass by reference instead (this avoids making a copy and uses the original instead):
void readAPuzzle(array2d_t& grid)
这篇关于如何在C ++中使用嵌套向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!