Vector of vector question

B

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
 
C

Cy Edmunds

BCC said:
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.
 
D

Donovan Rebbechi

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,
 
D

Daniel T.

BCC said:
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.
 
J

Jon Bell

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 said:
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.
 
R

Ron Natalie

BCC said:
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.
 
D

David Harmon

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));
 
G

Gavin Deane

David Fisher said:
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.
 
G

Gavin Deane

David Harmon said:
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.
 
D

Daniel T.

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...
 
R

RanggaPratama

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.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.
 
C

Clark Cox

RanggaPratama said:
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.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 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));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top