我是c ++的新手,我想写一个程序来生成一个整数数组.我一直在收到错误
test[i][j]=i;
invalid types 'int[int]' for array
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这里有什么问题?提前致谢.
int main()
{
int rows;
int cols;
cin>>rows>>cols;
int test[rows][cols];
get_test(rows,cols,&test[0][0]);
cout<<test[1][1]<<endl;
return 0;
}
int get_test(int rows,int cols,int *test)
{
int h=rows;
int w=cols;
int i=0,j=0;
for(i=0;i<h;i++)
{
for (j=0;j<w;j++)
{
test[i][j]=i;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Jar*_*d42 11
int test[rows][cols]; 非编译时值是一个可变长度数组,它是某些编译器的可能扩展.
更喜欢使用std::vector:
int get_test(std::vector<std::vector<int>>& test)
{
for (int i = 0;i != test.size(); ++i)
{
for (int j = 0; j != test[i].size(); ++j)
{
test[i][j] = i;
}
}
return 0;
}
int main()
{
int rows;
int cols;
cin>>rows>>cols;
std::vector<std::vector<int>> test(rows, std::vector<int>(cols));
get_test(test);
cout << test[1][1] << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
159 次 |
| 最近记录: |