vector of vectors

N

Nancy Keuss

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.
 
V

Victor Bazarov

Nancy Keuss said:
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?

By value:

void myfunction(vector<vector<int> > v);

By reference:

void myotherfunction(vector<vector<int> > &vr);

By a const reference:

void mythirdfunction(vector<vector<int> > const &vc);

To make a definition of those functions, replace the semicolon with
the function body. Don't forget to make it so 'vector' is a known
type name (include <vector>, declare using...)

Victor
 
J

Jonathan Turkanis

Victor Bazarov said:
By value:

void myfunction(vector<vector<int> > v);

By reference:

void myotherfunction(vector<vector<int> > &vr);

By a const reference:

void mythirdfunction(vector<vector<int> > const &vc);

To make a definition of those functions, replace the semicolon with
the function body. Don't forget to make it so 'vector' is a known
type name (include <vector>, declare using...)

You should mention that the second or third options are generally
better than first, unless you specifically need to make a copy.
Copying vectors of vectors can be expensive.

Also, 'using namespace std' is probably best done within functions,
not at namespace scope.

Jonathan.
 
V

Victor Bazarov

Jonathan Turkanis said:
You should mention that the second or third options are generally
better than first, unless you specifically need to make a copy.

You should have also mentioned that unless one needs to change the
vector in the function, it's better to use a const reference...
Copying vectors of vectors can be expensive.

Also, 'using namespace std' is probably best done within functions,
not at namespace scope.

Nobody suggested that. Only declare 'using' what you're actually
using.

Victor
 
J

Jonathan Turkanis

Victor Bazarov said:
You should have also mentioned that unless one needs to change the
vector in the function, it's better to use a const reference...

True enough. I wasn't trying to be exhaustive, but then, you probably
weren't either. :)
Nobody suggested that. Only declare 'using' what you're actually
using.

That can still lead to polution.

Jonathan
 
N

Nick Hounsome

Victor Bazarov said:
By value:

void myfunction(vector<vector<int> > v);

By reference:

void myotherfunction(vector<vector<int> > &vr);

By a const reference:

void mythirdfunction(vector<vector<int> > const &vc);

To make a definition of those functions, replace the semicolon with
the function body. Don't forget to make it so 'vector' is a known
type name (include <vector>, declare using...)

Victor

Use typedefs to make it clearer:

typedef std::vector<int> IntVec;
typedef std::vector<IntVec> IntMatrix;

void myfunction(const IntMatrix & v);

This cuts down on the typing and, when properly documented, decouples the
abstract type (A 2D matrix) from its
implementation (a vector of vectors)
 
V

Victor Bazarov

Nick Hounsome said:
Use typedefs to make it clearer:

typedef std::vector<int> IntVec;
typedef std::vector<IntVec> IntMatrix;

void myfunction(const IntMatrix & v);

This cuts down on the typing and, when properly documented, decouples the
abstract type (A 2D matrix) from its
implementation (a vector of vectors)


This is utterly misleading. A vector of vectors is not a matrix
(we're talking C++ Standard Library here, not mathematics). Each
vector in a "matrix" can have its own different size.

Victor
 
C

Cy Edmunds

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.
 
N

Nick Hounsome

Victor Bazarov said:
This is utterly misleading. A vector of vectors is not a matrix
(we're talking C++ Standard Library here, not mathematics). Each
vector in a "matrix" can have its own different size.

That is, sort of, my point, when you see vector< vector<int> > in the code
you cannot tell from
that alone whether that is a modelling decision or an implementation
decision.

It could be that he wanted a matrix and this is the easiest implementation
(for variable size)
Even if he specifically wanted the ability to have variable length 'rows'
but even here if you use the typedef
you can change to another implementation without altering the compiled code
(e.g. maybe a sparse matrix implemented
using a class and a std::map).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top