C++ 将两个 int 数组连接成一个更大的数组
问题描述:
有没有办法在 C++ 中取两个 int 数组
Is there a way to take two int arrays in C++
int * arr1;
int * arr2;
//pretend that in the lines below, we fill these two arrays with different
//int values
然后将它们组合成一个更大的数组,其中包含两个数组的值?
and then combine them into one larger array that contains both arrays' values?
答
int * result = new int[size1 + size2];
std::copy(arr1, arr1 + size1, result);
std::copy(arr2, arr2 + size2, result + size1);
只是建议,vector
作为动态数组会比指针更好
Just suggestion, vector
will do better as a dynamic array rather than pointer