471,585 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Vector of vector question

BCC
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,
B

Jul 22 '05 #1
12 2217
"BCC" <br***@akanta.com> wrote in message
news:gv*****************@newssvr27.news.prodigy.co m...
If I create a vector of vectors of double:

std::vector< std::vector<double> > table1;

Are my vectors of doubles uninitialized?

No. They are default initialized. You have an empty vector of empty vectors.
Do I have to loop through table1
and initialize each vector of doubles using new?
No. Please.

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.

Thanks,
B


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2
In article <gv*****************@newssvr27.news.prodigy.com> , BCC wrote:
If I create a vector of vectors of double:

std::vector< std::vector<double> > 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)
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<double> > table1 (m,n);
std::cout << table1.size() << std::endl; // 10
std::cout << table1[0].size() << std::endl; // 5
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/
Jul 22 '05 #3
"BCC" <br***@akanta.com> wrote:
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?


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.
Jul 22 '05 #4
In article <gv*****************@newssvr27.news.prodigy.com> ,
BCC <br***@akanta.com> wrote:
If I create a vector of vectors of double:

std::vector< std::vector<double> > 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.
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<double> > table1 (numRows,
std::vector<double>(numCols));

Then fill the table using the usual table1[row][col] notation.
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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #5
"Daniel T." <po********@eathlink.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-lit...html#faq-16.17 if you
didn't already have it.

David F
Jul 22 '05 #6

"BCC" <br***@akanta.com> wrote in message news:gv*****************@newssvr27.news.prodigy.co m...
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?
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.

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.
Jul 22 '05 #7
On Tue, 13 Jan 2004 01:37:24 GMT in comp.lang.c++, "Daniel T."
<po********@eathlink.net> 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<double> > table1(rows, vector<double>(columns));

Jul 22 '05 #8
"David Fisher" <no****@nospam.nospam.nospam> wrote in message news:<jj******************@nasal.pacific.net.au>.. .
"Daniel T." <po********@eathlink.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-lit...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-lit...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
Jul 22 '05 #9
David Harmon <so****@netcom.com> wrote in message news:<40***************@news.west.earthlink.net>.. .
On Tue, 13 Jan 2004 01:37:24 GMT in comp.lang.c++, "Daniel T."
<po********@eathlink.net> 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<double> > 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
Jul 22 '05 #10
de*********@hotmail.com (Gavin Deane) wrote:
"David Fisher" <no****@nospam.nospam.nospam> wrote in message
"Daniel T." <po********@eathlink.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-lit...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-lit...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...
Jul 22 '05 #11
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.

Jul 22 '05 #12
In article
<17******************************@localhost.talkab outprogramming.com>,
"RanggaPratama" <ma*******@hotmail.com> wrote:
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.
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<T>(col, 1));
}

Jul 22 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jessica | last post: by
10 posts views Thread by Stefan Höhne | last post: by
12 posts views Thread by No Such Luck | last post: by
24 posts views Thread by toton | last post: by
2 posts views Thread by danielhdez14142 | last post: by
9 posts views Thread by Jess | last post: by
6 posts views Thread by jmsanchezdiaz | last post: by
13 posts views Thread by prasadmpatil | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.