传递内嵌双数组作为方法参数 [英] Passing inline double array as method argument
本文介绍了传递内嵌双数组作为方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑方法
functionA (double[] arg)
我想通过一个双阵列内联,如
I want to pass a double array inline, like
functionA({1.9,2.8})
而不是首先创建一个数组,然后通过它,像
and not create an array first and then pass it, like
double var[] = {1.0,2.0};
functionA(var);
这可能与C ++?听起来很简单,但我找不到反正关于我的问题,这使我怀疑:)提示。
Is this possible with C++? Sounds simple, but I could not find a hint anyway concerning my question which made me suspicious :).
推荐答案
您可以做到这一点的std :: initializer_list<>
#include<vector>
void foo(const std::initializer_list<double>& d)
{ }
int main()
{
foo({1.0, 2.0});
return 0;
}
其中编译和下g ++的对我的作品
-std =的C ++ 0x
指定。
Which compiles and works for me under g++ with -std=c++0x
specified.
这篇关于传递内嵌双数组作为方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文