C ++:我如何传递一个变量的多个参数? [英] C++: how do I pass a variable for multiple arguments?

查看:120
本文介绍了C ++:我如何传递一个变量的多个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

传递不同类型的参数的可变数量 - C ++

我有一个函数,它接受n个参数。

I have a function that takes n arguments.

我想像这样调用它: function_name(number_args - 1,input,args_from_console [],output)

我如何做?

*接受n个参数的函数已经写入并且工作...我只是不想硬编码传递的变量。

*the function that takes n arguments already is written and works... I just don't want to hard code the variables being passed in.

编辑: (添加代码)

struct fann *ann = fann_create_standard(num_layers, 
                                            Config::NUMBER_OF_INPUT_NEURONS,
                                            Config::WIDTH_IN_BITS, 
                                            Config::WIDTH_IN_BITS, 
                                            Config::NUMBER_OF_OUTPUT_NEURONS);

上面的函数可以有至少3个参数...所需的参数是num_layers,num_input和num_output)

the function above can have at least 3 arguments... the required arguments are num_layers, num_input, and num_output)

可选参数是神经网络的隐藏层(他们被称为不重要....但基本上...它可能看起来像this:

the optional arguments are the hidden layers of the neural network (what they are call isn't important.... but basically... it could look like this:

fann_create_standard(#layers, #input, #hidden1, #hidden2, #hidden3, #hidden4, ... #output);

我想要做的是传递命令行参数来改变许多层,以及每个隐藏层的值是什么(这个函数调用中的中间参数),这样我不必在每次我想重新配置网络时重新编译程序。 p>

what I want to be able to do, is pass in command line arguments to change how many layers, and what the values of each of the hidden layers are (the middle arguments in this function call), so that I don't have to re-compile the program every time I want to re-configure the network.

推荐答案

重写函数,所以它不需要它有一个向量,而不是一个数组,可以从size()函数中检索。

Rewrite the function so it doesn't need that. Have it take a vector, instead of an array, then the number of arguments can be retrieved from the size() function.

如果我正确理解,你想调用的函数看起来像这样(忽略参数类型,为了简单起见,只是使用所有的int)

If I understand you correctly, the function you want to call looks something like this(Ignore the argument types, I'm just using all ints for simplicity's sake)

int func(int n, int input, int args[], int & output);

而且你不想弄清楚n。只需换行函数即可:

And you don't want to have to figure out n. Just wrap the function, thusly:

int func_wrap(int input, std::vector<int> & args, int & output)
{
    return func(args.size() - 1, input, &args[0], output);
}

这篇关于C ++:我如何传递一个变量的多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆