C++ 二维数组到一维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19913596/
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
C++ 2D array to 1D array
提问by Anton Antonov
I am attempting to convert a 2D array to 1D. I'm extremely new to C/C++ but I think it's very important to learn how to convert a 2D array to 1D. So here I am stumbling upon this problem.
我正在尝试将 2D 数组转换为 1D。我对 C/C++ 非常陌生,但我认为学习如何将 2D 数组转换为 1D 非常重要。所以在这里我偶然发现了这个问题。
My code so far is http://ideone.com/zvjKwP
到目前为止我的代码是 http://ideone.com/zvjKwP
#include<iostream>
using namespace std;
int main()
{
int n=0,m=0; // 2D array nRow, nCol
int a[n][m];
int i,j; // цикъл въвеждане 2D
int q,p,t; // for 2D=>1D
int b[100];
int r; // for cout
cout<<"Enter the array's number of rows and columns: ";
cin>>n>>m;
// entering values for the 2D array
for (i = 0;i<=n;i++)
{
for (j = 0;j<=m;j++)
{
cout<<"a["<<i<<"]["<<j<<"]="<<endl;
cin>>a[i][j];
cin.ignore();
}
}
// Most likely the failzone IMO
for (q = 0;q<=i;q++)
{
for (t = 0;t<=i*j+j;t++)
{
b[t] = a[i][j];
}
}
// attempting to print the 1D values
cout<<"The values in the array are"<<"\n";
for(r=0;r<=t;r++)
{
cout<<"b["<<r<<"] = "<<b[r]<<endl;
}
cin.get();
return 0;
}
I wrote a comment at where I think I fail.
我在我认为我失败的地方写了一条评论。
I must also limit the numbers that get into the 1D to numbers who's value^2 is greater than 50. But for sure I must solve the problem with the conversion 2D=>1D Can you help me out?
我还必须将进入 1D 的数字限制为 value^2 大于 50 的数字。但我肯定必须解决转换 2D=>1D 的问题你能帮我吗?
采纳答案by Boris Strandjev
You are right with your supposition:
你的假设是对的:
The cycle should be like:
循环应该是这样的:
for (q = 0; q < n; q++)
{
for (t = 0; t < m; t++)
{
b[q * m + t] = a[q][t];
}
}
It is always easier to consider such conversions from the view point of the higher dimension array. Furthermore with your code you did not actually modify i
or j
in the b
assigning cycle, so you should not expect different values to be assigned to the different array members of b
.
从更高维数组的角度考虑这种转换总是更容易。此外,对于您的代码,您实际上并未修改i
或j
在b
分配周期中,因此您不应期望将不同的值分配给b
.
回答by spellmansamnesty
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/arrays/
In that link look at the section on pseudo-multidimensional arrays.
在该链接中查看伪多维数组部分。
I've seen many examples that that get the subscripting algorithm wrong. If in doubt, trace it out. The order of sub-scripting a 2D array should go sequentially from 0-(HEIGHT*WIDTH-1)
我已经看到很多例子表明下标算法是错误的。如有疑问,请追查。二维数组的下标顺序应该从 0-(HEIGHT*WIDTH-1) 开始
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT * WIDTH];
int n,m;
int main ()
{
for (n=0; n<HEIGHT; n++)
for (m=0; m<WIDTH; m++)
{
jimmy[n*WIDTH+m]=(n+1)*(m+1);
}
}
回答by Vlad from Moscow
This code
这段代码
int n=0,m=0; // 2D array nRow, nCol
int a[n][m];
is invalid. First of all the dimensions shall be constant expressions and there is no sense to set them to 0.
是无效的。首先,维度应该是常量表达式,将它们设置为 0 是没有意义的。
And the more simple way to do your task is to use pointer. For example
完成任务的更简单方法是使用指针。例如
int *p = b;
for ( const auto &row : a )
{
for ( int x : row ) *p++ = x;
}
回答by Shubham
First of all, the size of the 1D array should be n*m
.
首先,一维数组的大小应该是n*m
.
The cycle can be as follows-
循环可以如下-
int lim = n*m;
for(q = 0; q<lim; ++q) {
b[q] = a[q/m][q%m];
}