passing a function a non decided amount of arguments/parameters

Hi there, I am wanting to have a function that i call from main that i want to pass a certain amount of arguments that i can change the amount of, ie- the first time i run it, it can have 1 argument, the second time, 5 arguments. I was thinking I could do this possibly with a vector but i can not seem to get it to work correctly, wondering if someone could help out, or simply tell me if this is even possible. This is the code i am thinking

1
2
3
4
5
6
7
8
9
10
11
12
 int calc_A(std::vector<int>amount)
{
	//loop the vector
	return 
}

int main()
{
  std::cout << calc_A(9,9,9) << '\n';
  std::cout << calc_A(9,9,9,9,9) << '\n';
  return 0;
}
Well you could use the code below, but I'm not sure that it's what you actually want to do. You need to be more specific. It would be more natural (to my eyes) that the passed container be an initializer_list rather than a vector, but the latter still works.

Please provide more detail of your intended use.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <numeric>

int calc_A( std::vector<int> amount )
{
   return std::accumulate( amount.begin(), amount.end(), 0 );
}

int main()
{
   std::cout << calc_A( { 1, 2, 3 } ) << '\n';
   std::cout << calc_A( { 9, 9, 9, 9, 9 } ) << '\n';
  
   std::vector<int> something{4, 5, 6, 7};
   std::cout << calc_A( something ) << '\n';
}


There are plenty of other ways of having variable names of arguments: default parameters, variadic templates, overloaded functions etc. Without knowing what your intended application is it is difficult to advise.
Last edited on
I believe you can do something similar to what you're attempting. I'm not well versed in vectors but I believe that they're similar to arrays. But I do believe to use a vector in a function parameter you have to actually pass a vector to it with the data set. So instead of passing 3 or 5 9s, youd have to create the vector set the data to the elements and then pass the whole thing. Then loop through the elements.

You could probably even use a separate function to set the data. Hence pass a string of numbers or even a int like 99999 then break it down into individual data elements then set then passed on to the function that takes the vector. This would probably be easier to do with a char array.
Last edited on
Well you could do this, but I'm not sure that it's what you actually want to do. You need to be more specific. It's more naturally (to my eyes) an initializer_list than a vector, but it still works.

Please provide more detail of your intended use.


I dont really have an intended use, I am just learning c++, and was trying to think of a solution of passing multiple arguments that can change amount. So it doesnt specifically have to be a vector, thats just the one way i thought of
you need to study vectors. The above code by lastchance shows how to do what the OP asked, and without any sort of char array or weirdness. Its ok not to know vectors well but your advice is on par with doing it via a void* -- you can do it that way, but its a bit quaint. Note how he did not even directly create an object to run a group of constants.
Last edited on
@jonin we must have replied at the same time...

lol this isn't weird!! its so much more fun!!

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
#include <iostream>
#include <string>
#include <vector>
void SomeFunc(std::vector<int> aVec)
{
	std::vector<int>::iterator it;
	for (it = aVec.begin(); it < aVec.end(); it++)
	{
		std::cout << (char)*it << '\n';
	}
}

void CharToVec(char cHa[],int size)
{
	std::vector<int> vec;
	for (int i=0;i<size;i++)
		vec.insert(vec.end(), cHa[i]);
	SomeFunc(vec);
}


int main()
{
	char cha[6] = "99999";
	CharToVec(cha, sizeof(cha));
}




this is how i learn doing weird stuff like this. I like doing stuff like this bc i like to think about the possibility of doing something 100x instead of just once...now obviously this isn't perfect bc the int is stored as the ascii code for 9. This would probably cause issues if a person needed to do math with the stored ints, but this could probably be overcome by some kinda conversion b4 the chars are set to the vector. Vectors are pretty cool. This was a good little practice for me. Thanks dude.
Last edited on
Topic archived. No new replies allowed.