Passing multiple vectors to a function
Sep 21, 2013 at 10:38pm UTC
How do I pass more than one vector to a function?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//Prototype
void getNum (std::vector<int > *numbers);
int main()
{
std::vector<int > numbers, morenumbers;
getNum(&numbers);
return 0;
}
void getNum(std::vector<int > *numbers)
{
int array[2] = {1,7};
numbers->assign (array, array+2);
}
This works fine, builds the vector the same as the array but I dont get how I can pass more than one vector to the getNum function, I am unsure of the syntax any ideas?
Last edited on Sep 21, 2013 at 10:40pm UTC
Sep 21, 2013 at 11:19pm UTC
Consider std::vector<std::vector<int >>
.
Plan B: You pass a pointer to function. Pointer points to a memory location. Where does a pointer+1 point to?
Sep 21, 2013 at 11:45pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//Prototype
void getNum (std::vector<int >&,std::vector<int >&);
int main()
{
std::vector<int > numbers, morenumbers;
getNum(numbers);
return 0;
}
void getNum(std::vector<int > &newNumbers,std::vector<int > &newmoreNumbers)
{
int array[2] = {1,7};
newNumbers.push_back (array, array+2);
}
Ok I have it working but are the parameters too long or is this the shortest way to do it?
Last edited on Sep 22, 2013 at 12:02am UTC
Topic archived. No new replies allowed.