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