如何将不同大小的数组传递给我的c ++函数?
我不确定这是否可行,但我愿意接受.
I'm not sure if this is possible but i'm open to ideas.
我有一个c ++函数.它会对用户定义为输入的数组进行一些计算,并将生成两个输出值.我们称之为"myfunction",它看起来像这样:
I have a c++ function. It does some calculation to arrays that are being defined by users as inputs and will generate two output values. Let's call it "myfunction" and it looks something like this:
void myfunction(double array1 [18],double array [9],double array [2],double * output1,double * output2)
void myfunction(double array1[18],double array[9],double array [2],double *output1, double *output2)
我想使此函数更通用,以便能够接受不同大小的数组.意思是,我希望该函数的用户能够定义其他大小的数组输入.
I want to make this function more generic so that it is able to take in arrays of different size. Meaning, I want users of this function to be able to define array inputs of other size.
例如,
void myfunction(double array1 [81],double array [27],double array [3],double * output1,double * output2)
void myfunction(double array1[81],double array[27],double array [3],double *output1, double *output2)
从数学上讲,尽管数组大小,"myfunction"仍能够计算出准确的输出.我不想复制"myfunction",因为用户可以将总共5组不同的数组大小定义为输入.
Mathematically, "myfunction" is able to calculate accurate outputs despite the array size. I don't want to make a duplicate of "myfunction" because there are a total of 5 different sets of array size that the user can define as inputs.
有什么想法吗?预先谢谢你
Any ideas? thank you in advance
您可以传递std::vector
或使用template
:
template <std::size_t N1, std::size_t N2, std::size_t N3> // and so on...
void work_with_arrays(double (&arr1)[N1], double (&arr2)[N2], double (&arr3)[N3]){
std::cout << "First arr of a size: " << N1 << "\n";
std::cout << "Second arr of a size: " << N2 << "\n";
std::cout << "Third arr of a size: " << N3 << "\n";
};
int main() {
double something[32];
double fewer[13];
double maybe_byte[8];
work_with_arrays(something, fewer, maybe_byte);
}
代码输出:
First arr of a size: 32
Second arr of a size: 13
Third arr of a size: 8
说明:
应该知道类型T[]
(T
是任何类型)的自变量会衰减 到T*
.如果我们要处理的是指向数组的指针,那么我们就没有关于其长度的信息,这是非常不幸的,因为固定长度数组的长度在编译时是已知的,并且在我们使用它们的任何地方都可以看到.
One should know that the argument of type T[]
(T
being any type) decays to T*
. If we're dealing with a pointer to an array, we have no information about its length, which is quite unfortunate, given the fact that lengths of fixed-length arrays are known at compile time and could be visible everywhere we work with them.
还应该知道功能模板不是功能.它是用于创建函数的模板.对于以上示例中使用的每个N#
组,都会生成一个供我们使用的函数.
One should also know that a function template is not a function. It is a template used to create functions. For every different set of N#
used in the example above, there will be a function generated for our use.
该衰减问题的解决方法是什么?
我们应该将 reference 传递给T[]
,而不是传递原始的T[]
.这样,类型不会衰减,并且数组的大小将是已知的.
Instead of passing a raw T[]
, we should pass a reference to T[]
. That way the type does not decay and the size of the array will be known.
语法?
人们可能注意到T (&name)[some_size]
看起来至少很奇怪.括号是必需的,因为简单的T &name[size]
将被解释为T& name[size]
,它是引用的数组,而不是对数组的引用.
One could notice that T (&name)[some_size]
looks at least bizarre. The parentheses are required, since plain T &name[size]
would be interpreted as T& name[size]
, which is an array of references, not a reference to an array.
结论:
为了能够检测传递参数数组的大小,我们不想将自己限制在一种或两种情况下-我们想将其全部覆盖,因此我们使用template
生成具有所需值N#
的函数.
Being able to detect the size of a passed-as-argument array, we don't want to limit ourselves with one or two cases - we want to cover them all, so we use a template
to generate functions with N#
of needed value.