【发布时间】:2020-02-05 12:00:00
【问题描述】:
我正在尝试使用C++ 中的数组创建一个MxN 零矩阵。但有些值不会归零。请帮我解决这个问题。
#include <iostream>
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
int i,j,s[m][n]={0};
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<s[i][j]<<" ";
}
cout<<endl;
}
}
输入: 4 4
输出:
0 11097 1757549776 11097
0 0 0 0
0 0 0 0
0 0 0 0
预期输出:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
【问题讨论】:
-
s[m][n] = {0}甚至不是有效的 C++。尝试改用std::vector>std::vector<int> >。例如,std::vector<std::vector<int> > s(m, std::vector<int>(n))将为您提供您期望的行为。
标签: c++ multidimensional-array initialization variable-length-array