function and dynamic aray

hi eveybody.
i have a question.plz help me.
can i use dynamics aray for argumant in function,in another way i wanna have a function and i wanna the number of my argumant not be constan,how can i do that,its possible?
and i wanna the number of my argumant not be constan


what do you mean ? pass as many arguments as you want upon function call ? something like :
foo( 1, 2, 3, 4, 5, 6, 7, 23, 123, 0, 1, 3, 765, 4 );
i mean i wanna have a function and sometimes i give it 2argumant and sometimes 3 or...
This involves using variadic functions but it seems that is dangerous. Here's a way to avoid it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>

void func(std::vector<int>& p1, std::vector<double>& p2, std::vector<std::string>& p3)
{
	//here is used type void; obviously may use another C++ type
	
	int integer1 = 7 * p1[2];	//using the 3rd integer to
	p1.push_back(integer1);		//pass it as the 4th in the p1[]
	
	double double1 = 3. * p2[0];//the same with the double
	p2.push_back(double1);		//in the p2[] values
	
	std::string proverb = p3[1] + p3[2] + p3[3];
	p3.push_back(proverb);		//and with the strings
	
	//---------------your code-------------------
	
}

int main()
{
	std::vector<int> params1;	//declaring a list with integers(if necessary)
	params1.push_back(1);
	params1.push_back(-12);		//filling the list with some values
	params1.push_back(34);		//how many are necessary
	
	std::vector<double> params2;//declaring another type of variables
	params2.push_back(1.234);	//(if necessary)
	params2.push_back(2.345);	//filling it
	
	std::vector<std::string> params3;// if necessary another list of variables
	params3.push_back("Hello world!");// of string type
	params3.push_back("All is ");
	params3.push_back("well when ");
	params3.push_back("ends well!..");
	
	
	func(params1, params2, params3);//calling the function
	
	std::cout << params1[3] << '\t' << params2[2] << "\n\n";
        std::cout << params3[0] << "\n\n";
	std::cout << params3[4] <<'\n';//verifying that called function works
	
	//here you can change the values(and the number) of the variables in the lists
	//params1/params2/params3 for a new call of the function.
	
	return 0;
}
238	3.702

Hello world!

All is well when ends well!..
Last edited on
Topic archived. No new replies allowed.