gpt4 book ai didi

c++ - 在 C++ 中转置二维数组

转载 作者:行者123 更新时间:2023-11-27 22:43:03 25 4
gpt4 key购买 nike

我有一个二维静态数组,如下所示:

1 1 1 1 1 1 
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6

我正在尝试转置此数组的行和列:

1 2 3 4 5 6 
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6

我能够通过使用如下所示的一维数组来解决它:

for (int i = 0; i < 6; ++i)
for (int j = 0; j < 6; ++j)
copyArray[i * 6 + j] = array[j * 6 + i];

但是对于 10x10 的数组,我该怎么做呢?

有人可以帮我吗?

最佳答案

您是否在寻找类似的东西:

int array[10][10], copyArray[10][10];

... // (fill array here)

for (int i = 0; i < 10; ++i)
for (int j = 0; j < 10; ++j)
copyArray[j][i]= array[i][j];

关于c++ - 在 C++ 中转置二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46534547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com