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. 9 5945
"Nancy Keuss" <mu*****@aol.co m> 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(vect or<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
"Victor Bazarov" <v.********@com Acast.net> wrote in message: "Nancy Keuss" <mu*****@aol.co m> 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(vect or<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.
"Jonathan Turkanis" <te******@kanga roologic.com> wrote... "Victor Bazarov" <v.********@com Acast.net> wrote in message: "Nancy Keuss" <mu*****@aol.co m> 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(vect or<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
In article <XwZMb.47661$xy 6.116635@attbi_ s02>,
Victor Bazarov <v.********@com Acast.net> wrote: "Jonathan Turkanis" <te******@kanga roologic.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.CSU A.Berkeley.EDU
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:XwZMb.4766 1$xy6.116635@at tbi_s02... "Jonathan Turkanis" <te******@kanga roologic.com> wrote... "Victor Bazarov" <v.********@com Acast.net> wrote in message: "Nancy Keuss" <mu*****@aol.co m> 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(vect or<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
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:rMYMb.4722 6$xy6.116719@at tbi_s02... "Nancy Keuss" <mu*****@aol.co m> 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(vect or<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<Int Vec> IntMatrix;
void myfunction(cons t 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)
"Nick Hounsome" <nh***@blueyond er.co.uk> wrote... "Victor Bazarov" <v.********@com Acast.net> wrote in message news:rMYMb.4722 6$xy6.116719@at tbi_s02... "Nancy Keuss" <mu*****@aol.co m> 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(vect or<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<Int Vec> IntMatrix;
void myfunction(cons t 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
"Nancy Keuss" <mu*****@aol.co m> wrote in message
news:d0******** *************** ***@posting.goo gle.com... 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/
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:%d0Nb.4753 8$na.37452@attb i_s04... "Nick Hounsome" <nh***@blueyond er.co.uk> wrote... "Victor Bazarov" <v.********@com Acast.net> wrote in message news:rMYMb.4722 6$xy6.116719@at tbi_s02... "Nancy Keuss" <mu*****@aol.co m> 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(vect or<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<Int Vec> IntMatrix;
void myfunction(cons t 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 "stereolithography" file (*.STL)
into my program. This has the following syntax...
Begin STL Snippet
**********
|
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 doubles using new?
And in cleaning up, manually delete each of these vectors of doubles?
Thanks,
|
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 foo containing vectors of the size
3. I then want to compare a vector<double> of size 3 (coord) with foo to find
a sequence of elements in foo that equals coord. However, when I try
this it returns when just one of the element of coord equals one...
|
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 "push_back" a copy of this into a
vector V. This is repeated many times in an iterative process.
Ok whenever I "push_back" a copy of Class A, I also want to assign a
pointer contained in an exisiting instance of a Class B to this
|
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 the vector with the
values I need (I know these values before hand).
- What's the best way to initialize the vector<vector<int> >? Can I
initilize it by enumerating its values?
- If I do: v = new vector<vector<int> >(3) for example, is it
| |
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;
};
|
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
detailed example of what I want:
Given a vector containing an arbitrary number of vectors, each of which
contains an arbitrary number of elements, generate a new vector in
which each element consists of one element taken from its corresponding...
|
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 the vector of vectors according to the median value of one the dimensions.
I tried to use the stl partition method like this:
// S is a vector of vectors containing the points
vector < vector <int> >::iterator iter;
|
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 " << v1.size( ) << endl;
v1.clear( ); //clears the vector
I have a few questions:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |