From here we have some difficulty in figuring out what you need the function to do, so I'm making some assumptions here that may be in error.
For example, this block:
1 2 3 4 5
|
srand(time(0));
vector<int> vList;
int size = vList.size();
for (int i = 0; i <= rand() % 10 + 1; i++)
vList.push_back(rand() % 10);
|
It seems you intend to make a function that creates vector<int> of random size, filled with random entries.
There is a minor problem before we can build the function, though. The for loop has a test that itself is going to choose random values at each loop. While this may well work, it is an odd formation. Typically one would choose the random value before the loop once, and use that for the loop's termination clause.
To be clear, in the for loop the clause " i <= rand() % 10 + 1" creates a new random value at each loop. Now, it is true that this produces random results, but this works better this way:
1 2 3 4 5 6
|
srand(time(0));
vector<int> vList;
int limit = rand() % 10 + 1;
vList.reserve( limit + 1 );
for (int i = 0; i <= limit; i++)
vList.push_back(rand() % 10);
|
In this version, I omit the "int size" statement. It would always be zero for a new vector, and isn't used elsewhere.
Instead, the limit is calculated in advance, used as the loop's termination clause, and as a bonus it allows the reserve of space in the vector before the loop which improves efficiency (well, as habit, when the volume is larger in future work).
So, to make a function of this you must decide if you want to return a vector, or if you want the caller to provide an empty vector. Returning a vector works, like any other value, but the implication is that the vector is copied. Modern compilers make this issue evaporate through optimization, but generally it's a good idea for large containers to be given as parameters. Something like:
1 2 3 4 5 6 7 8
|
void make_rand_vec( vector<int> & vList )
{
srand(time(0));
int limit = rand() % 10 + 1;
vList.reserve( limit + 1 );
for (int i = 0; i <= limit; i++)
vList.push_back(rand() % 10);
}
|
This version assume the vector<int> is created by the caller, provided to the function, and due to the & (a reference) is not a copy. There's a minor problem, though, which we can turn into a potential benefit. vList.reserve assumes the vector is empty. Is that required? If so, we may need to empty the vList before the reserve. On the other hand, if you'd like this function to add more integers to a vector<int> that is NOT empty, then the reserve may need to be altered or removed. Perhaps "vList.reserve( vList.size() + limit + 1 )".
These decisions are part of your design choice.
You may also want to return bool, which is merely true if the function has no trouble, false if there's an error.
Apply that thinking to the other blocks and let's see how far you get.