简体   繁体   English

C ++将2维整数数组插入另一个2维整数数组中

[英]C++ insert 2-dimensional array of integer into another 2-dimensional array of integer

I want to insert 2-dimensional array into another 2-dimensional array- both are integer. 我想将二维数组插入另一个二维数组 - 两者都是整数。 The first one is smaller than the second then there is no error of size. 第一个小于第二个,然后没有大小错误。 The bigger have data for example to the middle of its own and second part has no data. 较大的数据例如在它自己的中间,第二部分没有数据。 I want to insert the second array in the middle of these data so that I need to push down the data in the bigger, meaning to copy non-zero part over the zero data . 我想在这些数据的中间插入第二个数组,这样我就需要在较大的数据中下推数据,这意味着要在零数据上复制非零部分。 It would be appreciated if someone could offer related code in the most efficient way. 如果有人能够以最有效的方式提供相关代码,将不胜感激。 for example: 例如:

int A[4][2] = {{1, 2} , {3, 4} , { 0, 0} , {0, 0} };
int B[2][2] = {{5, 6} , {7, 8}};

I want to insert B into A (between the first and second row) and push down the second row into the third row. 我想将B插入A(第一行和第二行之间)并将第二行向下推入第三行。 Then we have: 然后我们有:

 int A[4][2] = {{1, 2} ,{5, 6} , {7, 8} , {3, 4} };

I want to do this without using nested loop. 我想这样做而不使用嵌套循环。

Arrays in C++ are FIXED SIZE -- so there's no way 'push down' data into an array, changing its size. C ++中的数组是固定大小的 - 因此无法将数据“下推”到数组中,从而改变其大小。 You can only copy stuff, overwriting (part of) the destination array, but leaving it the same size. 您只能复制内容,覆盖(部分)目标数组,但保留相同的大小。

If you want to do this, you need to either use something (like std::vector ) that allows for a changing size, or create a NEW array of the desired size and copy the data into it: 如果你想这样做,你需要使用允许改变大小的东西(比如std::vector ),或者创建一个所需大小的新数组并将数据复制到其中:

int C[6][2];
std::copy(A, A+2, C);
std::copy(B, B+2, C+2);
std::copy(A+2, A+4, C+4);

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

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