简体   繁体   English

如何将对数组的引用作为函数参数传递给数组,数组的大小仅在运行时才知道?

[英]How do I pass a reference to an array, whose size is only known at run-time, as a function parameter?

I have an array that is being dynamically created based on user input, and I need to pass that array to a function where it will be manipulated and the results passed back out to the original array. 我有一个根据用户输入动态创建的数组,我需要将该数组传递给一个函数,在该函数中对其进行操作,然后将结果传递回原始数组。 When I try to use 当我尝试使用

void MyFunction(int (&MyArray)[])

my compiler complains that it doesn't know how big the array is. 我的编译器抱怨它不知道数组有多大。

You can't. 你不能 You could use a std::vector though. 您可以使用std::vector

您可以提供一个指向数组第一个元素的指针+一个保存数组大小的第二个参数。

If it's just an array, why not pass the array itself and its size as a second parameter? 如果它只是一个数组,为什么不将数组本身及其大小作为第二个参数传递呢? (by passing the array as an int* or int[] , same thing as far as C++ is concerned). (通过将数组作为int*int[]传递,就C ++而言是相同的)。 As the value of the variable containing your array is only the pointer to the first element of your array, you don't end up killing your runtime by copying the contents of the array, but just a pointer which is as small as you can get in this case. 由于包含数组的变量的值只是指向数组第一个元素的指针,因此您不会通过复制数组的内容而最终终止运行时,而只是一个尽可能小的指针在这种情况下。

void MyFunction( int MyArray[], int size ) { /* edit the array */ }
int main() {
    // read nrElements
    // ...

    // create the array
    int *a = new int[nrElements];
    // populate it with data
    // ...
    // and then
    MyFunction(a, nrElements);
}

You should use a std::vector only if you want to resize the array in your function (eg add new elements to it), but otherwise you can just stick to this approach because it's faster. 仅在要在函数中调整数组大小(例如,向其中添加新元素)时,才应使用std::vector ,但否则可以坚持使用此方法,因为它更快。

Btw, the only case you would need a reference to an array is if you want to change the value of the variable you pass in when you call the function, as in, make it point to a different array. 顺便说一句,您唯一需要引用数组的情况是您想更改调用函数时传递的变量的值,例如,使其指向另一个数组。 Like in: 像:

void createMyArray(int* &array, int nrElements) {
    array = new int[nrElements];
    for (int i = 0; i < nrElements; ++i) {
        array[i] = 0;
    }
}

int *a = (int []) {1, 2, 3};
std::cout << a[0] << std::endl; // prints 1
createMyArray(a, 10);
// now  a  points to the first element of a 10-element array
std::cout << a[0] << std::endl; // prints 0

But you said the array is already created before providing it to the function, there's no point in using references. 但是您说过,在将数组提供给函数之前已经创建了数组,使用引用没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在函数的内部创建指针数组,该函数的大小直到运行时才知道 - How to create an array of pointers inside a function whose size is not known till run time 引用在运行时确定的大小数组 - reference to an array of size determined at run-time 如何传递以“运行时确定的大小”为参考的动态分配的数组? - How to pass a dynamically allocated array with “size determined at run time” as a reference? 使用 int 作为模板参数,直到运行时才知道 - Using an int as a template parameter that is not known until run-time 模板参数可以是运行时已知的 object 的类型吗? - Can a template parameter be the type of an object that will be known at run-time? 如何实现/使用:固定大小的动态数组的类(仅在运行时已知) - How to implement/use: class for a dynamic array of fixed size (known only at run time) 当C ++中的数组大小未知时,如何在运行时将对字符串数组的引用作为函数参数传递? - How to pass reference to a string array as a function parameter in runtime when array size is unknown in C++? 在运行时链接期间如何从DLL调用函数? - How do I call a function from my DLL during run-time linking? 如何在运行时设置常量配置参数 - How do I set constant configuration parameters at run-time 如何将对二维数组的引用传递给函数? - How do I pass a reference to a two-dimensional array to a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM