C++:复制数组

jac*_*ore 3 c++

是否有可能在C++中做这样的事情(现在不能自己测试)?

int myarray[10] = {111,222,333,444,555,666,777,888,999,1234};

void functioncc()
{
 int temparray = myarray;
 for(int x=0; x<temparray.length; x++){
    .... do something
 }

}
Run Code Online (Sandbox Code Playgroud)

也许这个(但我不认为是):

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

void functioncc(int arid)
{
  temparray[10] = "array"+arid;
  ........

}
Run Code Online (Sandbox Code Playgroud)

我可以在JavaScript中做类似的事情,但就像我说的那样 - 不要认为它在C++中是可能的.

谢谢你的时间.

Ton*_*nyK 8

#include <cstring>

int temparray[10] ;
memcpy (temparray, myarray, sizeof (myarray)) ;
Run Code Online (Sandbox Code Playgroud)

  • `<memory>`不是`memcpy`的正确标题,请尝试`<cstring>`或`<string.h>`.在C++中,很多人更喜欢来自`<algorithm>`的`std :: copy`. (4认同)
  • @Charles:在这种情况下,很多人仍然喜欢`memcpy`,因为MSVC在使用`std :: copy`时给出了令人讨厌的弃用(sic)警告...... (2认同)