二维数组 C++(输入和打印)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26726156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:40:39  来源:igfitidea点击:

2D array C++ (Input and Print)

c++arraysmultidimensional-array

提问by coolest111

//Variable Description
int H,W;
int map[H][W];


//Input
cin>>W>>H;

for(int i=0; i<H; i++)
{
    for (int j=0; j<W; j++)
    {
       cin>>map[i][j];
    }

}

cout<<endl;
//Print
for(int i=0; i<H; i++)
{
    for (int j=0; j<W; j++)
    {
        cout<<map[i][j];
    }

}
cout<<endl;
return 0;

`

`

My output is:

我的输出是:

3 3
1 2 3
4 5 6
7 8 9

789789789

Why i am not getting all the numbers in my output ? Why just the last line?

为什么我的输出中没有得到所有数字?为什么只有最后一行?

回答by gsamaras

You are initializing the sizes of the array after declaring it!!!

您在声明数组后初始化数组的大小!!!

You can't use variable length arrays in C++. Read thisanswer for more.

你不能在 C++ 中使用变长数组。阅读答案以了解更多信息。

so, you should use a std::vector, like this:

所以,你应该使用 a std::vector,像这样:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int H, W;
  cin >> W >> H;
  std::vector< std::vector<int> > map;
  map.resize(H); // H rows
  for(int i = 0; i < H; ++i)
    map[i].resize(W); // in every row, create W columns
  // thus this is equivalent to a HxD array

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> map[i][j];
    }

  }

  cout << endl;
  //Print
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << map[i][j] << " ";
    }

  }
  cout << endl;
  return 0;
}

You could do it with an array, but you should use dynamic allocation of memory, which requires you to de-allocate the memory when you are done. Here is how it should go:

您可以使用数组来完成,但您应该使用动态内存分配,这需要您在完成后取消分配内存。这是它应该如何进行:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;

  int** map = new int*[H];
  for(int i = 0; i < H; ++i)
    map[i] = new int[W];

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> map[i][j];
    }

  }

  cout << endl;
  //Print
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << map[i][j] << " ";
    }

  }
  cout << endl;


  // DON'T FORGET TO FREE
  for(int i = 0; i < H; ++i) {
    delete [] map[i];
  }
  delete [] map;
  return 0;
}

回答by jez

Your statement int map[H][W];intends to clear enough space to hold all the numbers. But it doesn't know how much space yet (W and H are undefined at the point where you declare map). So it will clear some random (possibly zero, possibly ridiculously large) amount of space, depending on what the memory slots occupied by H and W happen to contain. To allocate memory dynamically, you will need to do it (a) afterH and W are known, and (b) using the newkeyword (look it up using a search term like "dynamic memory allocation in C++", and while you're there, find out why it's also important to delete [] mapwhen you've finished with it).

您的陈述int map[H][W];旨在腾出足够的空间来容纳所有数字。但它还不知道有多少空间(W 和 H 在您声明的地方未定义map)。因此,它将清除一些随机(可能为零,可能大得离谱)的空间量,具体取决于 H 和 W 占用的内存插槽恰好包含的内容。要动态分配内存,您需要 (a)已知 H 和 W之后执行此操作,以及 (b) 使用new关键字(使用诸如“C++ 中的动态内存分配”之类的搜索词进行查找,并且当您在在那里,找出为什么delete [] map当你完成它时它也很重要)。