[英]what is the structure of a for loop that will output the contents of a multidimensional array in c++
我需要看一個示例,說明如何輸出多維數組。
string** row = new string*[level];
for(int i = 0; i < level; ++i) {
row[i] = new string[level];
}
// outputting:
int x; // filled with some value
int y; // filled with some value
如何通過y
然后x
打印row[y][x]
?
首先,您應該考慮使用std :: vector而不是手動動態分配,因為您使用的是C ++:
std::vector<std::vector<std::string>> rows(level);
代替
string** row = new string*[level];
並以這種方式初始化它:
for (std::vector<std::string>& row_vec : rows)
{
row_vec.resize(level);
}
並對其進行迭代,只需使用嵌套的for循環即可:
for (uint32_t x(0); x < level; ++x)
{
for (uint32_t y(0); y < level; ++y)
{
std::cout << "rows[" << x << "][" << y << "] = " << rows[x][y] << std::endl;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.