473,379 Members | 1,167 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,379 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 10541
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.