[英]what is the structure of a for loop that will output the contents of a multidimensional array in c++
I need to see an example of how one would go about outputting a multidimensional array. 我需要看一个示例,说明如何输出多维数组。
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
How would I print row[y][x]
by going through y
then x
? 如何通过
y
然后x
打印row[y][x]
?
First you should maybe consider using std::vector instead of manual dynamic allocation since you are using C++: 首先,您应该考虑使用std :: vector而不是手动动态分配,因为您使用的是C ++:
std::vector<std::vector<std::string>> rows(level);
instead of 代替
string** row = new string*[level];
and initialize it this way: 并以这种方式初始化它:
for (std::vector<std::string>& row_vec : rows)
{
row_vec.resize(level);
}
and to iterate over it just use nested for loops: 并对其进行迭代,只需使用嵌套的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.