首先,这不是 C++ 中数组声明/初始化的正确语法。我不知道是否有任何 IDE 可以为您可视化一个数组,但是您可以在代码中使用两个这样的 for 循环来完成它。这也显示了数组的正确语法。
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
int myArray[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i=0; i<3; ++i) {
for (int j=0; j<3; ++j)
cout << myArray[i][j] << ' ';
cout << endl;
}
return 0;
}
或者,如果你想方便调试,你可以像这样定义一个预处理器指令
#include <iostream>
#include <iomanip>
using namespace std;
#define test_array(name,ni,nj,w) \
cout << #name " = {\n"; \
for (int i=0; i<ni; ++i) { \
cout << " "; \
for (int j=0; j<nj; ++j) \
cout << setw(w+1) << myArray[i][j]; \
cout << endl; \
} \
cout << '}' << endl;
int main(int argc, char **argv)
{
int myArray[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
test_array(myArray,3,3,2)
return 0;
}
第四个参数允许你设置列宽,这样你就可以有很好的对齐方式。