C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Vector of vector question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
BCC
Guest





PostPosted: Tue Jan 13, 2004 1:05 am    Post subject: Vector of vector question Reply with quote



If I create a vector of vectors of double:

std::vector< std::vector 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,
B



Back to top
Cy Edmunds
Guest





PostPosted: Tue Jan 13, 2004 1:27 am    Post subject: Re: Vector of vector question Reply with quote



"BCC" <bryan (AT) akanta (DOT) com> wrote

Quote:
If I create a vector of vectors of double:

std::vector< std::vector table1;

Are my vectors of doubles uninitialized?


No. They are default initialized. You have an empty vector of empty vectors.

Quote:
Do I have to loop through table1
and initialize each vector of doubles using new?

No. Please.

Quote:

And in cleaning up, manually delete each of these vectors of doubles?

No again. std::vector is a well designed class that doesn't require a lot of
handholding.

Quote:

Thanks,
B






--
Cy
http://home.rochester.rr.com/cyhome/



Back to top
Donovan Rebbechi
Guest





PostPosted: Tue Jan 13, 2004 1:36 am    Post subject: Re: Vector of vector question Reply with quote



In article <gvHMb.9111$wP1.6082 (AT) newssvr27 (DOT) news.prodigy.com>, BCC wrote:
Quote:
If I create a vector of vectors of double:

std::vector< std::vector table1;

Are my vectors of doubles uninitialized?

They're empty. table1.size() will produce 0 and table1[0].size results in
undefned behaviour (since there is no first element yet)

Quote:
Do I have to loop through table1
and initialize each vector of doubles using new?

You don't use new, the vector class manages its own storage. You use vector
member functions. If you want the vectors to have some entries, you need to
do something like this:
typedef std::vector<double>::size_type dvecsize;
dvecsize m = 10, n = 5;
std::vector< std::vector table1 (m,n);
std::cout << table1.size() << std::endl; // 10
std::cout << table1[0].size() << std::endl; // 5

Quote:
And in cleaning up, manually delete each of these vectors of doubles?

No, the destructor of the vector takes care of deallocating storage. That's
the main point of having a vector.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/

Back to top
Daniel T.
Guest





PostPosted: Tue Jan 13, 2004 1:37 am    Post subject: Re: Vector of vector question Reply with quote

"BCC" <bryan (AT) akanta (DOT) com> wrote:

Quote:
If I create a vector of vectors of double:

std::vector< std::vector 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?

Any doubles created by the vector will be initialized to 0.0, they don't
need to be 'new'ed nor 'delete'ed.

Are you sure you want to use a vector of vectors? I would only do that
if I needed a ragged array. If the array represents a table, you would
be better off creating a 2D array class. See the FAQ for a sample
implementation.

Back to top
Jon Bell
Guest





PostPosted: Tue Jan 13, 2004 1:40 am    Post subject: Re: Vector of vector question Reply with quote

In article <gvHMb.9111$wP1.6082 (AT) newssvr27 (DOT) news.prodigy.com>,
BCC <bryan (AT) akanta (DOT) com> wrote:
Quote:
If I create a vector of vectors of double:

std::vector< std::vector table1;

Are my vectors of doubles uninitialized?

In fact, at this point, you have *no* vector<double>s at all. The "outer"
vector that is supposed to contain vector<double>s has zero size. No
memory has been allocated at all for storing vector<double>s.

Quote:
Do I have to loop through table1
and initialize each vector of doubles using new?

Assuming you know how big the table is supposed to be (numRows x numCols)
at run time, before you declare the table, the easiest way is to make the
table the appropriate size when you declare it:

std::vector<std::vector table1 (numRows,
std::vector<double>(numCols));

Then fill the table using the usual table1[row][col] notation.

Quote:
And in cleaning up, manually delete each of these vectors of doubles?

No, std::vector's destructor will take care of any cleanup that is
necessary, in this case. If you had declared a vector of pointers, then
you would need to either delete the pointers individually or make sure
other pointers are pointing to the objects being pointed to, before the
vector goes out of scope. But you still wouldn't have to worry about
deleting the vector itself, because you didn't use new to create it.

--
Jon Bell <jtbellm4h (AT) presby (DOT) edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA

Back to top
David Fisher
Guest





PostPosted: Tue Jan 13, 2004 2:00 am    Post subject: Re: Vector of vector question Reply with quote

"Daniel T." <postmaster (AT) eathlink (DOT) net> wrote:

Quote:
Are you sure you want to use a vector of vectors? I would only do that
if I needed a ragged array. If the array represents a table, you would
be better off creating a 2D array class. See the FAQ for a sample
implementation.

The reference is
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.17 if you
didn't already have it.

David F



Back to top
Ron Natalie
Guest





PostPosted: Tue Jan 13, 2004 3:32 am    Post subject: Re: Vector of vector question Reply with quote


"BCC" <bryan (AT) akanta (DOT) com> wrote

Quote:
If I create a vector of vectors of double:

std::vector< std::vector table1;

Are my vectors of doubles uninitialized? Do I have to loop through table1
and initialize each vector of doubles using new?

There are no elements to initialize, you've created an empty vector of empty
vectors. However, if you were to give it a size arg (or resize it), then absent
an explicit value to the constructor or resize call, it will provide default initialized
values.


Quote:
And in cleaning up, manually delete each of these vectors of doubles?

No, the vector will take all the elements with them when they go.

Back to top
David Harmon
Guest





PostPosted: Tue Jan 13, 2004 9:48 am    Post subject: Re: Vector of vector question Reply with quote

On Tue, 13 Jan 2004 01:37:24 GMT in comp.lang.c++, "Daniel T."
<postmaster (AT) eathlink (DOT) net> was alleged to have written:
Quote:
Are you sure you want to use a vector of vectors? I would only do that
if I needed a ragged array. If the array represents a table, you would
be better off creating a 2D array class.

Well, I can't entirely agree. A vector of vectors is a quick and
cheerful way of getting a table sized at run time without having to
reinvent the wheel.

vector< vector table1(rows, vector<double>(columns));


Back to top
Gavin Deane
Guest





PostPosted: Tue Jan 13, 2004 1:22 pm    Post subject: Re: Vector of vector question Reply with quote

"David Fisher" <nospam (AT) nospam (DOT) nospam.nospam> wrote

Quote:
"Daniel T." <postmaster (AT) eathlink (DOT) net> wrote:

Are you sure you want to use a vector of vectors? I would only do that
if I needed a ragged array. If the array represents a table, you would
be better off creating a 2D array class. See the FAQ for a sample
implementation.

The reference is
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.17 if you
didn't already have it.

David F

The next FAQ shows the same thing using a vector of vectors to
implement the 2D array class.

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.18

Removes all the need for explicit memory management in the class. And
it should be easy to design the class so that it's impossible for the
individual vectors-within-a-vector to end up with different sizes.

--
hth
GJD

Back to top
Gavin Deane
Guest





PostPosted: Tue Jan 13, 2004 11:41 pm    Post subject: Re: Vector of vector question Reply with quote

David Harmon <source (AT) netcom (DOT) com> wrote

Quote:
On Tue, 13 Jan 2004 01:37:24 GMT in comp.lang.c++, "Daniel T."
[email]postmaster (AT) eathlink (DOT) net[/email]> was alleged to have written:
Are you sure you want to use a vector of vectors? I would only do that
if I needed a ragged array. If the array represents a table, you would
be better off creating a 2D array class.

Well, I can't entirely agree. A vector of vectors is a quick and
cheerful way of getting a table sized at run time without having to
reinvent the wheel.

vector< vector table1(rows, vector<double>(columns));

The potential problem is that careless code could end up altering the
sizes of some of the vector<double>s. Depending on your application,
you might want the robustness of a class that does not allow this to
happen.

--
GJD

Back to top
Daniel T.
Guest





PostPosted: Wed Jan 14, 2004 3:25 am    Post subject: Re: Vector of vector question Reply with quote

[email]deane_gavin (AT) hotmail (DOT) com[/email] (Gavin Deane) wrote:

Quote:
"David Fisher" <nospam (AT) nospam (DOT) nospam.nospam> wrote in message

"Daniel T." <postmaster (AT) eathlink (DOT) net> wrote:

Are you sure you want to use a vector of vectors? I would only do that
if I needed a ragged array. If the array represents a table, you would
be better off creating a 2D array class. See the FAQ for a sample
implementation.

The reference is
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.17 if you
didn't already have it.

David F

The next FAQ shows the same thing using a vector of vectors to
implement the 2D array class.

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.18

Removes all the need for explicit memory management in the class. And
it should be easy to design the class so that it's impossible for the
individual vectors-within-a-vector to end up with different sizes.

A better choice, of course would be to implement the 2D array using a
single vector. This also removes the need for explicit memory management
and is easer to design...

Back to top
RanggaPratama
Guest





PostPosted: Wed Feb 04, 2004 12:34 am    Post subject: Re: Vector of vector question Reply with quote

Now, how do we initialize the 2d vector?

I tried to intialize the 2d vector by doing:(but, it's not working)

for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
table1[i].push_back(1);

I knew it wouldn't work. I was just trying to do different things.

I was thinking of using iterator, but I don't have any idea how to do it with 2d vector.

Back to top
Clark Cox
Guest





PostPosted: Fri Feb 06, 2004 9:24 pm    Post subject: Re: Vector of vector question Reply with quote

In article
<17db7dfb5ad3326db75c954a1ab97081 (AT) localhost (DOT) talkaboutprogramming.com>,
"RanggaPratama" <madrasah2 (AT) hotmail (DOT) com> wrote:

Quote:
Now, how do we initialize the 2d vector?

I tried to intialize the 2d vector by doing:(but, it's not working)

for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
table1[i].push_back(1);

I knew it wouldn't work. I was just trying to do different things.

You're not doing anything to change the size of the outer vector.
Since you said "initialize, I assume that the vector is empty to begin
with, so table1[i] is undefined.

Quote:
I was thinking of using iterator, but I don't have any idea how to do it with
2d vector.


How about (where T is whatever type the inner vector contains):

{
table1.clear();
table1.resize(row, std::vector }


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.