473,236 Members | 1,388 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,236 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 2296
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jessica | last post by:
Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
12
by: No Such Luck | last post by:
Hi All: I'm not sure if this is the right place to ask this question, but I couldn't find a more appropriate group. This is more of a theory question regarding an algorithm implemented in C, not...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
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...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
7
by: nw | last post by:
Hi, We've been having a discussion at work and I'm wondering if anyone here would care to offer an opinion or alternative solution. Aparently in the C programming HPC community it is common to...
6
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then...
13
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.