Connecting Tech Pros Worldwide Help | Site Map

vector of vectors

  #1  
Old July 22nd, 2005, 06:12 AM
Nancy Keuss
Guest
 
Posts: n/a
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.
  #2  
Old July 22nd, 2005, 06:12 AM
Victor Bazarov
Guest
 
Posts: n/a

re: vector of vectors


"Nancy Keuss" <muse188@aol.com> wrote...[color=blue]
> 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?[/color]

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


  #3  
Old July 22nd, 2005, 06:12 AM
Jonathan Turkanis
Guest
 
Posts: n/a

re: vector of vectors


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message:[color=blue]
> "Nancy Keuss" <muse188@aol.com> wrote...[color=green]
> > I've created a vector of vectors of ints, and I want to pass it as[/color][/color]
a[color=blue][color=green]
> > parameter to a function. Is this possible, and if so, then what is[/color][/color]
the[color=blue][color=green]
> > syntax like for the function header and function prototype when I[/color][/color]
need[color=blue][color=green]
> > to specify the variable type?[/color]
>
> 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...)
>[/color]

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.


  #4  
Old July 22nd, 2005, 06:12 AM
Victor Bazarov
Guest
 
Posts: n/a

re: vector of vectors


"Jonathan Turkanis" <technews@kangaroologic.com> wrote...[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message:[color=green]
> > "Nancy Keuss" <muse188@aol.com> wrote...[color=darkred]
> > > I've created a vector of vectors of ints, and I want to pass it as[/color][/color]
> a[color=green][color=darkred]
> > > parameter to a function. Is this possible, and if so, then what is[/color][/color]
> the[color=green][color=darkred]
> > > syntax like for the function header and function prototype when I[/color][/color]
> need[color=green][color=darkred]
> > > to specify the variable type?[/color]
> >
> > 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...)
> >[/color]
>
> You should mention that the second or third options are generally
> better than first, unless you specifically need to make a copy.[/color]

You should have also mentioned that unless one needs to change the
vector in the function, it's better to use a const reference...
[color=blue]
> Copying vectors of vectors can be expensive.
>
> Also, 'using namespace std' is probably best done within functions,
> not at namespace scope.[/color]

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

Victor


  #5  
Old July 22nd, 2005, 06:12 AM
E. Mark Ping
Guest
 
Posts: n/a

re: vector of vectors


In article <XwZMb.47661$xy6.116635@attbi_s02>,
Victor Bazarov <v.Abazarov@comAcast.net> wrote:[color=blue]
>"Jonathan Turkanis" <technews@kangaroologic.com> wrote...[color=green]
>> Also, 'using namespace std' is probably best done within functions,
>> not at namespace scope.[/color]
>
>Nobody suggested that. Only declare 'using' what you're actually
>using.[/color]

And preferably not in a header file.
--
Mark Ping
emarkp@soda.CSUA.Berkeley.EDU
  #6  
Old July 22nd, 2005, 06:12 AM
Jonathan Turkanis
Guest
 
Posts: n/a

re: vector of vectors


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:XwZMb.47661$xy6.116635@attbi_s02...[color=blue]
> "Jonathan Turkanis" <technews@kangaroologic.com> wrote...[color=green]
> > "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message:[color=darkred]
> > > "Nancy Keuss" <muse188@aol.com> wrote...
> > > > I've created a vector of vectors of ints, and I want to pass[/color][/color][/color]
it as[color=blue][color=green]
> > a[color=darkred]
> > > > parameter to a function. Is this possible, and if so, then[/color][/color][/color]
what is[color=blue][color=green]
> > the[color=darkred]
> > > > syntax like for the function header and function prototype[/color][/color][/color]
when I[color=blue][color=green]
> > need[color=darkred]
> > > > 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[/color][/color][/color]
with[color=blue][color=green][color=darkred]
> > > the function body. Don't forget to make it so 'vector' is a[/color][/color][/color]
known[color=blue][color=green][color=darkred]
> > > type name (include <vector>, declare using...)
> > >[/color]
> >
> > You should mention that the second or third options are generally
> > better than first, unless you specifically need to make a copy.[/color]
>
> You should have also mentioned that unless one needs to change the
> vector in the function, it's better to use a const reference...[/color]

True enough. I wasn't trying to be exhaustive, but then, you probably
weren't either. :-)
[color=blue][color=green]
> > Also, 'using namespace std' is probably best done within[/color][/color]
functions,[color=blue][color=green]
> > not at namespace scope.[/color]
>
> Nobody suggested that. Only declare 'using' what you're actually
> using.
>[/color]

That can still lead to polution.

Jonathan


  #7  
Old July 22nd, 2005, 06:12 AM
Nick Hounsome
Guest
 
Posts: n/a

re: vector of vectors



"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:rMYMb.47226$xy6.116719@attbi_s02...[color=blue]
> "Nancy Keuss" <muse188@aol.com> wrote...[color=green]
> > 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?[/color]
>
> 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
>
>[/color]

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)


  #8  
Old July 22nd, 2005, 06:12 AM
Victor Bazarov
Guest
 
Posts: n/a

re: vector of vectors


"Nick Hounsome" <nh002@blueyonder.co.uk> wrote...[color=blue]
>
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:rMYMb.47226$xy6.116719@attbi_s02...[color=green]
> > "Nancy Keuss" <muse188@aol.com> wrote...[color=darkred]
> > > 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?[/color]
> >
> > 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
> >
> >[/color]
>
> 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)[/color]


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


  #9  
Old July 22nd, 2005, 06:12 AM
Cy Edmunds
Guest
 
Posts: n/a

re: vector of vectors


"Nancy Keuss" <muse188@aol.com> wrote in message
news:d084b908.0401131232.7cca9a1e@posting.google.c om...[color=blue]
> 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.[/color]

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.

--
Cy
http://home.rochester.rr.com/cyhome/


  #10  
Old July 22nd, 2005, 06:13 AM
Nick Hounsome
Guest
 
Posts: n/a

re: vector of vectors



"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:%d0Nb.47538$na.37452@attbi_s04...[color=blue]
> "Nick Hounsome" <nh002@blueyonder.co.uk> wrote...[color=green]
> >
> > "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> > news:rMYMb.47226$xy6.116719@attbi_s02...[color=darkred]
> > > "Nancy Keuss" <muse188@aol.com> wrote...
> > > > 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[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > syntax like for the function header and function prototype when I[/color][/color][/color]
need[color=blue][color=green][color=darkred]
> > > > 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
> > >
> > >[/color]
> >
> > 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[/color][/color]
the[color=blue][color=green]
> > abstract type (A 2D matrix) from its
> > implementation (a vector of vectors)[/color]
>
>
> 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.
>[/color]

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).
[color=blue]
> Victor
>
>[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
dereferencing a vector of vectors Caudata answers 4 December 6th, 2007 12:12 AM
Using for_each with a vector of vectors PolkaHead answers 3 November 29th, 2006 12:35 AM
partition a vector of vectors acosgaya answers 0 October 6th, 2006 04:11 PM
Sizing a vector of vectors cheeser answers 3 July 19th, 2005 07:32 PM