【发布时间】:2013-05-30 19:02:12
【问题描述】:
您好,我想从 on 到 10 个参数传递给一个将保存在数组中的函数。
function( 4, 3, 5); //calling function and passing arguments to it.
void function(int array[10])
{
cout<<array[0]; // = 4
cout<<array[1]; // = 3
cout<<array[2]; // = 5
cout<<array[3]; // = NULL or 0 or sth else
}
基本上,我希望有机会传递尽可能多的参数,不多也不少。
不可能是这样的。
function( 4, 3, 5); //calling function and passing arguments to it.
void function(int x1=NULL , int x2=NULL , int x3=NULL ,int x4=NULL , int x5=NULL)
{
for (int i=0 ; i<10;i++)
{
array[i] = x1; // x2 , x3 and so on ...
}
cout<<array[0]; // = 4
cout<<array[1]; // = 3
cout<<array[2]; // = 5
cout<<array[3]; // = NULL or 0 or sth else
}
程序比这个例子复杂,所以我需要它是数组。
【问题讨论】:
-
使用向量可以让程序不那么复杂
-
我不确定我在这里看到了问题。
-
那么你想传递变量参数并返回数组中的参数?
-
我想像单个整数一样传递参数,但我想将它们保存到数组中。如果我将 x 、 a 、 b 传递给函数,它会将其保存到数组中,例如 array[0]=x 、 array[1]=b 、array[2]=a
-
我不确定你的要求可以做到。就像嫌疑人说的那样, std::vector 可能是你最好的选择。不过我可能误解了你的问题,你能详细说明一下吗?
标签: c++ arrays function arguments