问题描述
我放弃了将二维数组传递给函数的尝试,因为在编译时维度未知.经过一些研究,似乎二维向量可能是一个很好的替代品.我只想确认这是声明维度 totalRows X totalColumns 的二维向量的正确方法,初始化每个单元格以包含空格字符:
I gave up on trying to pass a 2d array to a function, where the dimensions are not known at compile time. After some research, it seems like a 2d vector may be a good substitute. I just want to confirm that this is the correct way to declare a 2d vector of dimension totalRows X totalColumns, initializaing each cell to contain the space character:
vector<vector<char> > world(totalRows, vector<char>(totalColumns, ' '));
推荐答案
您的代码有效.正如评论中所注意到的,vector>
并不是真正的二维数组.
Your code works. As noticed in the comment, vector<vector<T>>
is not truly a 2D array.
这里重要的是 (totalRows, vector(totalColumns, ' '))
和 (totalColumns, vector(totalRows, ' '))
code> 是等效的,只要您始终遵循用于创建向量的规则:line x column
(或 column x line
)
The important thing here is that (totalRows, vector<char>(totalColumns, ' '))
and (totalColumns, vector<char>(totalRows, ' '))
are equivalent, as long as you always follow the rule used to create the vector: line x column
(or column x line
)
这篇关于二维向量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!