使用new运算符初始化数组 [英] Use new operator to initialise an array
本文介绍了使用new运算符初始化数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要初始化在使用逗号在大括号包围的元素分开格式的数组例如:
I want to initialise an array in the format that uses commas to separate the elements surrounded in curly braces e.g:
int array[10]={1,2,3,4,5,6,7,8,9,10};
不过,我需要使用new运算符分配内存例如:
However, I need to use the new operator to allocate the memory e.g:
int *array = new int[10];
有没有一种方法,以论文的方法结合起来,这样我可以使用new运算符分配内存,并用花括号初始化数组?
Is there a way to combine theses methods so that I can allocate the memory using the new operator and initialise the array with the curly braces ?
推荐答案
您可以使用的memcpy
分配后。
int originalArray[] ={1,2,3,4,5,6,7,8,9,10};
int *array = new int[10];
memcpy(array, originalArray, 10*sizeof(int) );
我不知道任何语法,可以让你做到这一点的自动的。
I'm not aware of any syntax that lets you do this automagically.
这篇关于使用new运算符初始化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文