我如何使用C ++中的递归制作副本数组函数

我已经坚持了整整一天。我正在尝试使用

void recCopy(int from[], int to[], int i) {

}

从复制

void iterCopy(int from[], int to[], int size) {

    for (int i = 0; i < size; ++i)

        to[i] = from[i];
}
评论
  • psunt
    psunt 回复

    i is the number of elements to copy, so

    • if i is zero, you're done
    • otherwise, copy one element, then copy i-1 elements from the "tails" of the arrays.

    在代码中:

    void recCopy(int from[], int to[], int i) 
    {
        if (i > 0)
        {
            *to = *from;
            recCopy(from + 1, to + 1, i - 1);
        }
    }