gpt4 book ai didi

c++ - 以另一种方式读取数组 2 维 C++

转载 作者:行者123 更新时间:2023-11-28 01:02:12 24 4
gpt4 key购买 nike

读取数组输入后:

std::vector<unsigned char *> inputs(elements);

inputs”是一个二维数组:inputs[3][2],然后读出这个数组,值为:

inputs[0][0]=1
inputs[0][1]=2
inputs[1][0]=3
inputs[1][1]=4
inputs[2][0]=5
inputs[2][1]=6

我想将这个二维数组读入一维数组:

int counter=0;
int * allElements = new int[6];

for(int i=0; i<3; i++)
for(int j=0; j<2; j++)
{
allElements[counter++] = (int)inputs[i][j];
}

这是将所有元素读入一维数组的传统方式,我相信如果我以这种方式读取“allElements”的元素:

for(int i=0; i<6; i++)
printf("%d ", allElements[i]);

它应该是:1 2 3 4 5 6

但是,我想将二维数组的所有元素读入一维数组,这样当我这样做时:

for(int i=0; i<6; i++)
printf("%d ", allElements[i]);

应该是:1 3 5 2 4 6

换句话说,首先读取二维数组的第一个元素。

我怎样才能做到这一点?

最佳答案

只需交换您的 ij 循环即可。

关于c++ - 以另一种方式读取数组 2 维 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8129084/

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