473,699 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing vector<vector<i nt> > 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<i nt> > 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<i nt> >? Can I
initilize it by enumerating its values?
- If I do: v = new vector<vector<i nt> >(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 10570
pmatos wrote:
I have a vector of vector of ints, I could use C approach by using
int[][] but I think C++ vector<vector<i nt> > 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<i nt> >? 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<i nt> >(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<i nt> >(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<i nt> > 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<i nt> >? 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<i nt> >? If I have a =
int[][] well initialized of course can I do v = vector<vector<i nt> >(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<i nt> >? If I have a =
int[][] well initialized of course can I do v = vector<vector<i nt> >(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.********@com Acast.net> wrote in message
news:yY******** ***********@new sread1.mlpsca01 .us.to.verio.ne t
pmatos wrote:
- If I do: v = new vector<vector<i nt> >(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<i nt> >(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
5811
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 in Mathematica(tm): someList={42,9,11};
3
2805
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 to achieve this? 1. vector<vector<int> > v(n); for(int i=0;i<n;i++){ v = vector<int>(m);
1
2086
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(',')); but it gives the weird messages:
1
2406
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
2897
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 (_x) {
2
3318
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 fault which I resolved by doing vector<vector<int example; example.push_back(vector<int>());
0
1133
by: citystud | last post by:
does C# support vector< vector<int> > ?if not how can i use vector?
10
5011
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++ Primer - 4/e * * exercise 9.20 * STATEMENT: * Write a program to to compare whether a vector<intcontains the
10
6061
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 remove the elements with odd values from your list * and the even values from your vector.
0
8689
marktang
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...
0
8618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9035
jinu1996
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...
0
7752
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
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...
0
5875
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();...
0
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2010
bsmnconsultancy
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...

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.