且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

传递数组作为函数参数在C ++中

更新时间:2023-11-11 17:12:58

的数组的内容被传递给函数。问题是:

The contents of the array are being passed to the function. The problem is:

n = sizeof(a) / sizeof(int)

不给你数组的大小。一旦你通过数组给一个函数你不能再得到它的大小。

Does not give you the size of the array. Once you pass an array to a function you can't get its size again.

由于您没有使用动态数组可以使用 的std ::阵列 这不记得它的大小。

Since you aren't using a dynamic array you can use a std::array which does remember its size.

您也可以使用:

template <int N>
void problem1(int (&a) [N]) 
{
    int size = N;
    //...
}