Returning vectors ints + strings from a function

How would I write the following with a vector? I have not had much experience with them yet and I am trying to learn :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <iostream>
#include <string>

//Prototype
getNum(vector<int>);

int main()
{
   vector<int> return_values(2); //creates a vector to hold 2 numbers (correct syntax?)
   getNum(return_values); //call getNum passing the vector to it.
}

int getNum(vector<int>)
{

   //Array
   int numbers[3] = {1,7,9} 
   //How do I now add this array to my vector and return it?
}




The second part is if I had multiple arrays in getNum can i specify how many vectors will be passed to it without writing it like.

int getNum(vector<int>,vector<int>,vector<int>)

Or is that pretty much it? and for the third part! is returning a string vector any different than an integer except changing the syntax from int to a string
Last edited on
Managed to solve this one myself, if anyone else needs it.

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

//Prototype
getNum(vector<int> *return_values);

int main()
{
   vector<int> return_values; //creates a vector to hold numbers
   getNum(&return_values); //call getNum passing the vector to it.
}

int getNum(vector<int> *return_values)
{

   //Array
   int numbers[3] = {1,7,9} 
   return_values->assign (numbers, numbers+3);
}


For anyone else looking at this code, please post if there is an easier or more effecient way of doing this? seems to be working great at the mo!

How do I pass more than one vector to the function?
Last edited on
Topic archived. No new replies allowed.