472,958 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Initializing vector<vector<int> > and other vector questions...

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
initializing the internal vector automatically?
- Is returning a vector from a function too heavy from an efficiency
perspective?
Or it would be better to return always a pointer to a vector?

Cheers,

Paulo Matos

Jul 23 '05 #1
5 10499
pmatos wrote:
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?
Not really. The simplest way would probably be initialising it from
an array of arrays. While you do have to create that array of arrays,
but later you don't need to manage it.
- If I do: v = new vector<vector<int> >(3) for example, is it
initializing the internal vector automatically?
Yes, it creates a vector of three empty vectors of int (provided that
your 'v' variable is a pointer). You could add another argument to
the initialiser and control how the "internal" vectors are initialised:

v = new vector<vector<int> >(3, vector<int>(5, 42));

That way it's a vector of 3 vectors of 5 int each, and ints are all
set to have the value 42.
- Is returning a vector from a function too heavy from an efficiency
perspective?
Not really. Your compiler probably has some kind of "return value
optimization" implemented.
Or it would be better to return always a pointer to a vector?


Depends on your needs.

V
Jul 23 '05 #2

Victor Bazarov wrote:
pmatos wrote:
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?
Not really. The simplest way would probably be initialising it from
an array of arrays. While you do have to create that array of

arrays, but later you don't need to manage it.

Thanks for you previous answers...
This issue is insteresting. Do you mean that if I create a int[][],
then I can use it to initialize the vector<vector<int> >? If I have a =
int[][] well initialized of course can I do v = vector<vector<int> >(a)
? (Being both v and a pointers of course.)

Paulo Matos

Jul 23 '05 #3
pmatos wrote:
...
This issue is insteresting. Do you mean that if I create a int[][],
then I can use it to initialize the vector<vector<int> >? If I have a =
int[][] well initialized of course can I do v = vector<vector<int> >(a)
? (Being both v and a pointers of course.)


No. Unfortunately, it doesn't work like that. You would still need
to do some dancing around.

After some thinking, I now conclude that I should take it back, there
is no easy way to initialise a vector of vectors from a two-dimensional
array. A vector can be initialised from an array by

vector<int> vi(array_of_int,
array_of_int + sizeof(array_of_int)/sizeof(int) );

but for a vector of vectors you'd have to write that initialiser for
each vector separately, which makes it not worth your while, probably.

V
Jul 23 '05 #4
No. Unfortunately, it doesn't work like that. You would still need
to do some dancing around.

After some thinking, I now conclude that I should take it back, there
is no easy way to initialise a vector of vectors from a two-dimensional array. A vector can be initialised from an array by

vector<int> vi(array_of_int,
array_of_int + sizeof(array_of_int)/sizeof(int) );
but for a vector of vectors you'd have to write that initialiser for
each vector separately, which makes it not worth your while, probably.
Well, yeah, It seems I'll have to give it some thinking then. Thanks a
lot for the help.

Paulo Matos
V


Jul 23 '05 #5
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:yY*******************@newsread1.mlpsca01.us.t o.verio.net
pmatos wrote:
- If I do: v = new vector<vector<int> >(3) for example, is it
initializing the internal vector automatically?


Yes, it creates a vector of three empty vectors of int (provided that
your 'v' variable is a pointer). You could add another argument to
the initialiser and control how the "internal" vectors are
initialised:
v = new vector<vector<int> >(3, vector<int>(5, 42));

That way it's a vector of 3 vectors of 5 int each, and ints are all
set to have the value 42.


Isn't one of the main reasons for using vector that it does memory
management for you? Is it not therefore almost always a bad idea to be
creating vectors using new?

--
John Carson

Jul 23 '05 #6

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

Similar topics

3
by: Andreas Krueger | last post by:
Hi! I am fed up with vector<int> iv; iv.push_back(42); iv.push_back(9); iv.push_back(11); ... and would rather use a function "fillVector": vector<int> iv = fillVector(42,9,11); like...
3
by: Erik Borgstr?m | last post by:
Hi, Yes, I have followed some of the discussion on vector<vector<int> >, but perhaps I'm just blind. I want to use a vector<vector<int> > of outer size n and inner size m. Are these correct ways...
1
by: Ingo Nolden | last post by:
Hi, I am using spirit 1.31 I have been trying the following example from the spirit docs. I tried it with int and double neither works: vector<int> v; rule<> r = list_p(int_p, ch_p(',')); ...
1
by: Alex Vinokur | last post by:
------ foo.cpp ------ #include <vector> using namespace std; int main() { const vector<int> v1 (10); const vector<bool> v2 (10); &v1;
10
by: Piotr | last post by:
I have a class 'Statistics' which has a private attribute ' vector<int>* _x;' And in the destructor of the Statistics, I have this code to free the memory: Statistics::~Statistics() { if...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
0
by: citystud | last post by:
does C# support vector< vector<int> > ?if not how can i use vector?
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.