跳至内容

ArrayCopy

ArrayCopy

它将一个数组复制到另一个数组中。

int  ArrayCopy(
   void&        dst_array[],         // destination array
   const void&  src_array[],         // source array
   int          dst_start=0,         // index starting from which write into destination array
   int          src_start=0,         // first index of a source array
   int          count=WHOLE_ARRAY    // number of elements
   );

参数

dst_array[]

[out] 目标数组

src_array[]

[in] 源数组

dst_start=0

[in] 目标数组的起始索引。默认起始索引为0。

src_start=0

[in] 源数组的起始索引。默认起始索引为0。

count=WHOLE_ARRAY

[in] 要复制的元素数量。默认情况下,整个数组会被复制(count=WHOLE_ARRAY).

返回值

返回被复制的元素数量。

注意

如果 count<=0 或 count>src_size-src_start,则复制数组的剩余部分。数组从左到右复制。对于序列数组,起始位置会根据从左到右复制的方式正确定义。如果数组复制到自身,结果将是未定义的。

如果数组的类型不同,在复制过程中会尝试将源数组的每个元素转换为目标数组的类型。字符串数组只能复制到字符串数组中。包含需要初始化的对象的类和结构数组不会被复制。结构数组只能复制到相同类型的数组中。

对于静态和动态数组(类和结构成员除外),目标数组的大小会自动增加到被复制的数据量(如果后者超过数组大小)。

示例:

void OnStart()
  {
//---
   int src_data[10];
   for (int i=0; i<ArraySize(src_data); i++) src_data[i]=i;
   int dst_data[];
   //--- copy data to dst_data[]
   ArrayCopy(dst_data,src_data,0,0,WHOLE_ARRAY);
   //--- print copied data[]
   PrintFormat("Copied array size=%d",ArraySize(dst_data));
   for (int i=0; i<ArraySize(dst_data); i++) PrintFormat("index=%d, value=%d",i,dst_data[i]);
  }
最后更新于