473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

vector of vectors

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.
Jul 22 '05 #1
9 5916
"Nancy Keuss" <mu*****@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 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
Jul 22 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message:
"Nancy Keuss" <mu*****@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 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...)


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.
Jul 22 '05 #3
"Jonathan Turkanis" <te******@kangaroologic.com> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message:
"Nancy Keuss" <mu*****@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 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...)


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
Jul 22 '05 #4
In article <XwZMb.47661$xy6.116635@attbi_s02>,
Victor Bazarov <v.********@comAcast.net> wrote:
"Jonathan Turkanis" <te******@kangaroologic.com> wrote...
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.


And preferably not in a header file.
--
Mark Ping
em****@soda.CSUA.Berkeley.EDU
Jul 22 '05 #5
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:XwZMb.47661$xy6.116635@attbi_s02...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message:
"Nancy Keuss" <mu*****@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 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...)


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


True enough. I wasn't trying to be exhaustive, but then, you probably
weren't either. :-)
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.


That can still lead to polution.

Jonathan
Jul 22 '05 #6

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:rMYMb.47226$xy6.116719@attbi_s02...
"Nancy Keuss" <mu*****@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 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


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)
Jul 22 '05 #7
"Nick Hounsome" <nh***@blueyonder.co.uk> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:rMYMb.47226$xy6.116719@attbi_s02...
"Nancy Keuss" <mu*****@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 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


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
Jul 22 '05 #8
"Nancy Keuss" <mu*****@aol.com> wrote in message
news:d0**************************@posting.google.c om...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #9

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:%d0Nb.47538$na.37452@attbi_s04...
"Nick Hounsome" <nh***@blueyonder.co.uk> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:rMYMb.47226$xy6.116719@attbi_s02...
"Nancy Keuss" <mu*****@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 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
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.


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).
Victor

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
12
by: BCC | last post by:
If I create a vector of vectors of double: std::vector< std::vector<double> > table1; Are my vectors of doubles uninitialized? Do I have to loop through table1 and initialize each vector of...
1
by: Dennis | last post by:
Hi I'm trying to implement a vector of vectors where find can be used to find a vector<double> in the vectors of vectors, that is hard to understand i guess. What I mean is that I got a vector...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
8
by: cayblood | last post by:
Hello, I have been interested in something kind of like the next_permutation from the STL algorithm library, except that I want it to find possible combinations of vector elements. Here is a more...
0
by: acosgaya | last post by:
hi, I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors. I am trying to partition...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.