473,909 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Vector of vector question

BCC
If I create a vector of vectors of double:

std::vector< std::vector<dou ble> > 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 2357
"BCC" <br***@akanta.c om> wrote in message
news:gv******** *********@newss vr27.news.prodi gy.com...
If I create a vector of vectors of double:

std::vector< std::vector<dou ble> > 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.c om>, BCC wrote:
If I create a vector of vectors of double:

std::vector< std::vector<dou ble> > 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<dou ble>::size_type dvecsize;
dvecsize m = 10, n = 5;
std::vector< std::vector<dou ble> > 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.c om> wrote:
If I create a vector of vectors of double:

std::vector< std::vector<dou ble> > 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.c om>,
BCC <br***@akanta.c om> wrote:
If I create a vector of vectors of double:

std::vector< std::vector<dou ble> > 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<dou ble>(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*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #5
"Daniel T." <po********@eat hlink.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.c om> wrote in message news:gv******** *********@newss vr27.news.prodi gy.com...
If I create a vector of vectors of double:

std::vector< std::vector<dou ble> > 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********@eat hlink.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******* ***********@nas al.pacific.net. au>...
"Daniel T." <po********@eat hlink.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.w est.earthlink.n et>...
On Tue, 13 Jan 2004 01:37:24 GMT in comp.lang.c++, "Daniel T."
<po********@eat hlink.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11438
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 values to a 2D vector, of which every vector has the same length. I tried the following but I get a "System.NullReferenceException" error when I ran it.
10
7090
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 clear(), in DOT.NET the capacity() is reduced to 0.
12
3171
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 necessarily a C language question. I'm trying to break up a vector into an arbitrary number of subvectors, equal (or as near to equal) in size as possible. My problem occurs when the vector is not evenly divisible by the number of subvectors...
24
2978
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 vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
2
3341
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>());
9
3767
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" contains built-in types (e.g. int), then "clear()" doesn't remove anything at all. Why does "clear()" have this behaviour? Also, when I copy one vector "v1" from another vector "v2", with "v1" longer than "v2" (e.g. "v1" has 2 elements and "v2" has...
7
3905
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 allocate multidimentional arrays like so: int *v_base = (int *) malloc(1000000*sizeof(int)); int **v = (int **) malloc(1000*sizeof(int *));
6
11635
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 push_back it, or could i pushback the two ints directly somehow? Thanks for all.
13
1939
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 this? I wrote the following program to test this out. #include <fstream> #include <iostream> #include <string>
6
7395
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", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
0
10037
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
11348
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10921
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...
1
11052
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10540
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8099
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
7249
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();...
1
4776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3359
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.