C ++传递一个数组,而不是一个可变长度的参数列表 [英] c++ Pass an array instead of a variable length argument list
问题描述
所以我有一个函数,采用可变长度的参数列表,例如:
So I have a function that takes a variable length argument list, for example:
int avg(int count,...){
//stuff
}
我可以用称它为AVG(4,2,3,9,4);
,它工作正常。它需要维持这种功能
I can call it with avg(4,2,3,9,4);
and it works fine. It needs to maintain this functionality.
有没有办法,我也有一个数组,而不是列出变量调用它呢?例如:
Is there a way for me to also call it with an array instead of listing the variables? For example:
AVG(4 myArray的[5])
使得功能平均
并没有看到有什么区别?
avg(4,myArray[5])
such that the function avg
doesn't see any difference?
推荐答案
没有没有这样的办法。但是,您可以让两个功能,一个采用可变数量的参数,以及一个带有一个数组(或更好,但一个的std ::矢量
)。第一个功能简单的包装参数到数组(或载体),并调用第二功能。
No there is no such way. You can however make two functions, one that takes a variable number of arguments, and one that takes an array (or better yet, an std::vector
). The first function simply packs the arguments into the array (or vector) and calls the second function.
这篇关于C ++传递一个数组,而不是一个可变长度的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!