Nancy Keuss said:
Hi,
I've created a vector of vectors of ints, and I want to pass it as a
parameter to a function. Is this possible, and if so, then what is the
syntax like for the function header and function prototype when I need
to specify the variable type?
Thank you,
N.
Nancy-
The others have said how to pass a vector. If that's what you really need to
do, you are all set. But I almost never pass a container in my own work.
Consider:
template <typename ITER>
double average(ITER first, ITER last)
{
double sum = 0.0;
int n = 0;
while (first != last)
{
sum += *first++;
++n;
}
return sum / n;
}
You call it as follows:
double xbar = average(my_vec.begin(), my_vec.end());
This example calculates the average of the values in a vector (my_vec) but
without actually passing my_vec as an argument. The advantage of this is
that the same function will work with just about any container type
(std::list, C array, etc.). In this particular case we were also able to
avoid assuming a vector of ints -- the function would work with a vector of
doubles or floats just as well. This is the general style of the Standard
Template Library.
Of course the STL type of interface may be inconvenient or even useless in
your application. In any event, good luck.