0
我目前卡住试图使用循环的范围基础来初始化我的数组。如何使用基于范围的for循环初始化2D数组为false使用基于范围的循环
我目前做了什么,但它没有在构造函数中使用C++ 11数组。构造函数是无效,不带任何参数
//sets everything to flase
for (size_t rowCount = 0; rowCount < NROWS; ++rowCount) {
for (size_t colCount = 0; colCount < NCOLS; ++colCount){
m_Floor[rowCount][colCount] = STARTING_PEN_POSITION;
}
}
这里是我迄今为止设置应有尽有假(起始笔位置)
for (auto const &row : m_Floor) {
for (auto const &column : row) {
//something = STARTING_PEN_POSITION;
}
}
,这阵是头文件里面
std::array <std::array <bool, NCOLS>, NROWS> m_Floor;
其中NCOLS是size_t的恒定静态,值为70
d NROWS是size_t的一个恒定值,其值为22
后'auto' .. –
只是为了确保拆除'const'之一如果我删除它,然后使用代码'm_Floor [row] [column] = STARTING_PEN_POSITION;'它不工作,除非我使用for循环的语法错误 – Aeroy
你会'column = STARTING_PEN_POSITION;',而不是'm_Floor [row] [column] = STARTING_PEN_POSITION;'。或者更合理的是,你会将'column'重命名为'cell',以明确它是对矩阵中单个单元格的引用。或者为了简单起见,你可以删除内部循环,只要执行(for(auto&row:m_Floor){std :: fill(std :: begin(row),std :: end(row),STARTING_PEN_POSITION); }'。 – ShadowRanger