简体   繁体   English

从1d数组创建2d数组

[英]creating 2d array from 1d arrays

if i have several arrays of the same datatype, what is the best way to copy them all into a 2d array. 如果我有几个相同数据类型的数组,那么将它们全部复制到二维数组中的最佳方法是什么。 for example 例如

int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[] = {9,8,7,6,5,4,3,2,1,0};

int array2d[][];
//pseudo code array2d = array1 + array2

so that 以便

array2d[0][0]; //=1 (first member of array1)
array2d[1][0]; //=9 (first member of array2)

considering an array is just a pointer to the first element, i thought I could do this, but it creates a compiler error. 考虑到数组只是指向第一个元素的指针,我认为我可以这样做,但它会产生编译错误。

array2d[0][0] = array1;
array2d[1][0] = array2;

I'm guessing I can't copy using references because an array needs its entries in contiguous memory? 我猜我不能使用引用进行复制,因为数组需要在连续内存中输入它? is there a memset like funciton I can use? 是否有像我可以使用的功能的memset?

Impossible. 不可能。 You need to copy element by element from one array to another. 您需要逐个元素地将元素从一个数组复制到另一个数组。

Also you can mimic 2d array with array of pointers to arrays of ints. 您还可以使用指向int数组的指针数组来模拟2d数组。

int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[] = {9,8,7,6,5,4,3,2,1,0};

int *array2d[2]; 

array2d[0] = array1;
array2d[1] = array2;

or this 或这个

int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[] = {9,8,7,6,5,4,3,2,1,0};

int *array2d[] = {array1, array2}; 

cout << "[0][0]=" << array2d[0][0] << endl;
cout << "[1][0]=" << array2d[1][0] << endl;

OR REVERSE 或者反过来

If your goal is to present 2d array to some API, then you should refactor your side. 如果你的目标是向某些API提供2d数组,那么你应该重构你的一面。 For example, you can mimic your 1d arrays with pointers: 例如,您可以使用指针模拟您的1d数组:

// an ampty array
int array2d[2][10];

// pointers to parts
int *array1 = array2d[0];
int *array2 = array2d[1];

int n;

// fill "arrays"
for(int i=0, n=1; i<10; ++i, ++n) {
    array1[i] = n;
}
for(int i=0, n=9; i<10; ++i, --n) {
    array2[i] = n;
}

// now you are ready
cout << "[0][0]=" << array2d[0][0] << endl;
cout << "[1][0]=" << array2d[1][0] << endl;

If you want an actual 2D array (contiguous in memory), you'll have to copy the elements. 如果你想要一个真正的2D数组(在内存中是连续的),你将不得不复制这些元素。 However, you could emulate it with an array of 2 pointers: 但是,您可以使用2个指针数组来模拟它:

int *array2d[2];
array2d[0] = array1;
array2d[1] = array2;

It's not an actual 2D array, but you could make array2d an array of pointers to the 1d arrays: 它不是一个真正的2D数组,但你可以使array2d成为一个指向1d数组的指针数组:

int* array2d[2];

array2d[0] = array1;
array2d[1] = array2;

Otherwise you'll have to copy the elements over manually. 否则,您必须手动复制元素。

There's a few things that you can do. 你可以做一些事情。 If you know that your data is going to be constant within each array, you can #define it, and then use it in your 1D and 2D arrays. 如果您知道每个数组中的数据都是常量,则可以#define它,然后在1D和2D数组中使用它。 Alternatively, you can memcpy the elements from the 1D array to the 2D array. 或者,您可以将1D数组中的元素存储到2D数组中。 The second point is illustrated here: 第二点如下所示:

#define ARRAY_1 { 1, 2, 3, 4, 5, 6 }
#define ARRAY_2 {7, 8, 9, 10, 11, 12 }

int array_1[] = ARRAY_1;
int array_2[] = ARRAY_2;

int two_dim_array[][] = {
    ARRAY_1,
    ARRAY_2,
}

Define and assign : 定义并分配

int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[] = {9,8,7,6,5,4,3,2,1,0};

int *array2d[2];
array2d[0] = array1;
array2d[1] = array2;

Test : 测试

printf("%d\t",array2d[0][0]);
printf("%d\t",array2d[0][9]);
printf("%d\t",array2d[1][5]);

it gives 1 10 4 它给出1 10 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM