在 C++ 中将一维数组转换为二维数组

发布于 2024-12-05 21:35:47 字数 584 浏览 0 评论 0原文

我有一个 49 空间的一维数组,声明为 int boardArray [49];,还有一个二维 7x7 数组,声明为 int boardArrayTwo [7][7]'我试图使用嵌套的 for 循环将一维数组放入二维数组中,这是我用来测试它的代码。

for (int i = 0; i > 50; ++i)
{
    boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
for (int x = 0; x >= 7; ++x)
{
    for (int k = 0; k >= 7; ++k)
    {
        for (int n = 0; n >= 49; ++n)
        {
            boardArrayTwo[x][k] = boardArray[n];
            cout << boardArrayTwo[x][k] << " " << endl;
        }

    }
}

我尝试运行这个但没有任何反应。我做错了吗?

I have a 49 space one dimensional array declared as int boardArray [49]; and I also have a two dimensional 7x7 array declared as int boardArrayTwo [7][7]' I am trying to use nested for loops to throw the one dimensional array into the two dimensional array here is the code I am using to test it.

for (int i = 0; i > 50; ++i)
{
    boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
for (int x = 0; x >= 7; ++x)
{
    for (int k = 0; k >= 7; ++k)
    {
        for (int n = 0; n >= 49; ++n)
        {
            boardArrayTwo[x][k] = boardArray[n];
            cout << boardArrayTwo[x][k] << " " << endl;
        }

    }
}

I tried running this but nothing happens. Am I doing it wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

为你拒绝所有暧昧 2024-12-12 21:35:47
for (int x = 0; x >= 7; ++x)
{
    for (int k = 0; k >= 7; ++k){
         for (int n = 0; n >= 49; ++n)
    {

这是错误的。 x 和 k 应该 < 7(并且不应该使用第三个循环):

for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k){
        boardArrayTwo[x][k] = boardArray[7*x + k];

编辑:

就像@Fabio Ceconello让我在他的评论中注意到,即使第一个循环由于反向条件检查而错误,也应该这样修改:

for (int i = 0; i < 49; ++i)
{
    boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
for (int x = 0; x >= 7; ++x)
{
    for (int k = 0; k >= 7; ++k){
         for (int n = 0; n >= 49; ++n)
    {

this is wrong. x and k should be < 7 (and the third cycle shouldn't be used) :

for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k){
        boardArrayTwo[x][k] = boardArray[7*x + k];

EDIT:

like @Fabio Ceconello make me notice in his comment, even the first loop is wrong because of the inverted condition checks, it should be modified this way:

for (int i = 0; i < 49; ++i)
{
    boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
旧时光的容颜 2024-12-12 21:35:47

除了循环中的反转逻辑(其他人提到的)之外,不需要第三个内部循环。只需将归因放在第二个内部循环中即可:

boardArrayTwo[x][k] = boardArray[x * 7 + k];

编辑:
我还应该提到,所有这些文字都不是好的做法,我在上面又添加了一个 (7)。我将重写代码如下:

#define arrlen(x) (sizeof(x)/sizeof((x)[0]))

for (int i = 0; i < arrlen(boardArray); ++i)
{
    boardArray[i] = i; 
}
int stride = arrlen(boardArrayTwo[0]);
for (int x = 0; x < arrlen(boardArrayTwo); ++x)
{
    for (int k = 0; k < stride; ++k)
    {
        boardArrayTwo[x][k] = boardArray[stride * x + k];
        cout << boardArrayTwo[x][k] << " " << endl;
    }
}

警告:如果此处未声明数组(作为参数传递),则 arrlen() 将不起作用。但这是另一个很长的故事了......

Apart from the inverted logic in the loops (which the others mentioned), there's no need for the third inner loop. Just put the attribution in the second inner loop:

boardArrayTwo[x][k] = boardArray[x * 7 + k];

EDIT:
I should also mention that all these literals aren't good practice, and I added one more (7) above. I'd rewrite the code as follows:

#define arrlen(x) (sizeof(x)/sizeof((x)[0]))

for (int i = 0; i < arrlen(boardArray); ++i)
{
    boardArray[i] = i; 
}
int stride = arrlen(boardArrayTwo[0]);
for (int x = 0; x < arrlen(boardArrayTwo); ++x)
{
    for (int k = 0; k < stride; ++k)
    {
        boardArrayTwo[x][k] = boardArray[stride * x + k];
        cout << boardArrayTwo[x][k] << " " << endl;
    }
}

caveat: if the arrays aren't declared here (were passed as parameters), arrlen() won't work. But that's another long story...

鼻尖触碰 2024-12-12 21:35:47

看起来您的目标数组是按行优先顺序排列的。您可以直接将源阵列爆炸到位。

memcpy(boardArrayTwo, boardArray, 49 * sizeof(int));

或者如果您更喜欢更惯用的 C++ 语言:

std::copy(boardArray, boardArray + 49, reinterpret_cast<int*>(boardArrayTwo));

It looks like your destination array is in row-major order. You could just blast the source array directly into place.

memcpy(boardArrayTwo, boardArray, 49 * sizeof(int));

or if you prefer something in more idiomatic C++:

std::copy(boardArray, boardArray + 49, reinterpret_cast<int*>(boardArrayTwo));
简单 2024-12-12 21:35:47

您使用了 i >; 50 在你的 for 循环中。应该是 i < 49 和所有其他循环相同。

另外,这行不通。您将所有 boardArrayTwo[][] 值设置为 boardArray[49] 您应该执行如下操作:

for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k)
    {
        boardArrayTwo[x][k] = boardArray[7*x + k];
        cout << boardArrayTwo[x][k] << " " << endl;
    }
}

int count = 0;

for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k)
    {
        boardArrayTwo[x][k] = boardArray[count];
        cout << boardArrayTwo[x][k] << " " << endl;
        count++;
    }
}

You used i > 50 in your for loop. It should be i < 49 and same for all the other loops.

Also, this won't work. You're setting all of the boardArrayTwo[][] values to boardArray[49] You should instead do something like this:

for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k)
    {
        boardArrayTwo[x][k] = boardArray[7*x + k];
        cout << boardArrayTwo[x][k] << " " << endl;
    }
}

or

int count = 0;

for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k)
    {
        boardArrayTwo[x][k] = boardArray[count];
        cout << boardArrayTwo[x][k] << " " << endl;
        count++;
    }
}
若有似无的小暗淡 2024-12-12 21:35:47

首先,for 循环中的第二项表示当条件为 true 时 for 循环将运行。因此,对于所有循环,您应该使用 < 而不是 >=

其次,n 上的循环是额外的,不应该存在。您需要的是遍历xk,然后将相应的元素从boardArray复制到boardArrayTwo

您可以执行以下操作之一:

int n = 0;
for (int x = 0; x < 7; ++x)
    for (int k = 0; k < 7; ++k)
    {
        boardArrayTwo[x][k] = boardArray[n];
        ++n;
    }

或使用公式来计算正确的 n

for (int x = 0; x < 7; ++x)
    for (int k = 0; k < 7; ++k)
        boardArrayTwo[x][k] = boardArray[x*7+k];

我写了 x*7+k 因为它看起来像 x迭代数组的行,每行有 7 个元素,表示 boardArray 的第 x*7+k 元素表示的位置 [x][k] boardArrayTwo/

First of all, the second term in the for loop says the for loop would run while that condition is true. So you should use < instead of >= for all your loops.

Second, the loop over n is extra and shouldn't be there. What you need is to go through x and k, then copy the corresponding element from boardArray to boardArrayTwo.

You could do one of these:

int n = 0;
for (int x = 0; x < 7; ++x)
    for (int k = 0; k < 7; ++k)
    {
        boardArrayTwo[x][k] = boardArray[n];
        ++n;
    }

or use a formula to calculate the proper n:

for (int x = 0; x < 7; ++x)
    for (int k = 0; k < 7; ++k)
        boardArrayTwo[x][k] = boardArray[x*7+k];

I wrote x*7+k because it seems like x is iterating over the rows of the array, each row having 7 elements, says that x*7+kth element of the boardArray represents position [x][k] of boardArrayTwo/

悲歌长辞 2024-12-12 21:35:47

请注意,

for (int i = 0; i > 50; ++i)

如果i初始化为0,它不会大于50,因此它永远不会进入循环。

Note

for (int i = 0; i > 50; ++i)

if i is initialized to 0, it won't be greater than 50 and thus it will never enter the loop.

記憶穿過時間隧道 2024-12-12 21:35:47

在每个循环中,您使用大于或等于 (>),而不是小于 (<) 或等于。您还应该注意到,正如 Fabio 上面指出的那样,第三个嵌套循环将 boardArrayTwo[x][k] 一遍又一遍地设置为 0-49,共 49 次。您需要使用算术来操作 x 和 k,以便它们成为 boardArray 的索引,然后将该索引分配给 boardArrayTwo[x][k]

使用 0..7(含)也很重要,这实际上是 8 个位置。你的数组的长度只有 7,所以你实际上最终会得到一些垃圾值。

#include <iostream>
using std::cout;
using std::endl;

int main () {

    int boardArray[49];
    int boardArrayTwo[7][7];

for (int i = 0; i < 50; ++i)
{
    boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k)
    {
            boardArrayTwo[x][k] = boardArray[x*7 + k];
            cout << boardArrayTwo[x][k] << " " << endl;
    }
}

}

运气好的话(除非我让自己难堪)这应该可以解决问题!

编辑:特别感谢法比奥!

In each of your loops you used greater than or equal (>) to rather than less than (<) or equal to. You should also notice that, as Fabio points out above, the third nested loop is setting boardArrayTwo[x][k] to 0-49 over and over again, 49 times. You will need to use arithmetic to manipulate x and k so that they will be an index into boardArray, and then assign that index to boardArrayTwo[x][k].

It's also important that you are using 0..7 inclusive, which is actually 8 positions. Your array are only of length 7 so you are actually ending up with some garbage values in there.

#include <iostream>
using std::cout;
using std::endl;

int main () {

    int boardArray[49];
    int boardArrayTwo[7][7];

for (int i = 0; i < 50; ++i)
{
    boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k)
    {
            boardArrayTwo[x][k] = boardArray[x*7 + k];
            cout << boardArrayTwo[x][k] << " " << endl;
    }
}

}

With any luck (unless I am embarrassing myself) this should do the trick!

EDIT: Special thanks to Fabio!

归途 2024-12-12 21:35:47
for(int i=0; i<49; i++)
b[i]=(i+1);

int p=0;
for(int i=0;i<7;i++){
        for(int j=0;j<7;j++)
        {a[i][j]=b[p];
        p++;}
        }

除了其他错误之外,第三个循环使您的代码错误

for(int i=0; i<49; i++)
b[i]=(i+1);

int p=0;
for(int i=0;i<7;i++){
        for(int j=0;j<7;j++)
        {a[i][j]=b[p];
        p++;}
        }

beside other errors, third loop is making your code wrong

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文