C ++将两个int数组连接成一个更大的数组 [英] C++ concatenate two int arrays into one larger array

查看:297
本文介绍了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];
copy(arr1, arr1 + size1, result);
copy(arr2, arr2 + size2, result + size1);

只是建议, vector 会做得更好动态数组而不是指针

Just suggestion, vector will do better as a dynamic array rather than pointer

这篇关于C ++将两个int数组连接成一个更大的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆