您可以使用vDSP_vclr
andvDSP_vadd
如下:
int sourceLength = 3;
float* source = (float*)malloc(sourceLength * sizeof(float));
// source is filled with data, let's say [5, 5, 5]
int destinationLength = 10;
float* destination = (float*)malloc(destinationLength * sizeof(float));
// destination is filled with ones so [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
// Prepare the destination array to receive the source array
// by setting its values to 0 in the range [initialIndex, initialIndex + N-1]
int initialIndex = 2;
vDSP_vclr((destination+initialIndex), 1, sourceLength);
// We add source[0, N-1] into destination[initialIndex, initialIndex + N-1]
vDSP_vadd(source, 1, (destination+initialIndex), 1, (destination+initialIndex), 1, sourceLength);
或者更简洁,您也可以使用 Brad Larson 所说的 'cblas_scopy'
// Init source and destination
// We copy source[0, sourceLength] into destination[initialIndex, initialIndex + sourceLength]
cblas_scopy(sourceLength, source, 1, (destination+initialIndex), 1);