473,327 Members | 1,920 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,327 software developers and data experts.

Clearing multidimensional STL vector


Hi!

I didn't get the idea of exactly *how* such an array of vectors work.

Consider this minimal code:
--------------------

#include <vector>
using namespace std;

int main ()
{
vector< vector<int> > vI;
vI.resize(10);
for(int i(0); i<10; i++)
{
vI[i].resize(3);
}

vI.clear();
}

---------------------

Will vI.clear() really free *all* vI[i], too?

Or will I need another loop just before calling vI.clear() to sequentially
delete all vI[i]: such as:

--------------
for(int i(0); i<10; i++)
{
vI[i].clear();
}
--------------

Thanks in advance

Yours

ST
Jul 23 '05 #1
3 2990
Sascha T. wrote:
Hi!

I didn't get the idea of exactly *how* such an array of vectors work.

Consider this minimal code:
--------------------

#include <vector>
using namespace std;

int main ()
{
vector< vector<int> > vI;

That is a "2-dimensional" vector.

vI.resize(10);

This makes the "rows" 10.
for(int i(0); i<10; i++)
{
vI[i].resize(3);
}

This makes the "columns" 3.

vI.clear();
}

---------------------

Will vI.clear() really free *all* vI[i], too?
It destroys all vector<int> objects contained in the vector.



Or will I need another loop just before calling vI.clear() to sequentially
delete all vI[i]: such as:

--------------
for(int i(0); i<10; i++)
{
vI[i].clear();
}

No, this is not needed.
Your code in better shape:
#include <vector>

int main ()
{
using namespace std;

vector<vector<int> > vI(10, vector<int>(3));
// Not needed here
// vI.clear();
}


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #2
Sascha T. wrote:

Hi!

I didn't get the idea of exactly *how* such an array of vectors work.

Consider this minimal code:
--------------------

#include <vector>
using namespace std;

int main ()
{
vector< vector<int> > vI;
vI.resize(10);
for(int i(0); i<10; i++)
{
vI[i].resize(3);
}

vI.clear();
}

---------------------

Will vI.clear() really free *all* vI[i], too?
It will destroy all vI[i], part of which is destroying all the elements in
each of them, so the answer is yes.
Or will I need another loop just before calling vI.clear() to sequentially
delete all vI[i]: such as:

--------------
for(int i(0); i<10; i++)
{
vI[i].clear();
}
--------------


No need for that.

Jul 23 '05 #3

Got it, and thank you for that:
vector<vector<int> > vI(10, vector<int>(3));

Cheers

ST
Jul 23 '05 #4

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

Similar topics

3
by: Peter L. | last post by:
It would be great if someone could give me a hint for my problem. I create a multidimensional array: vector< vector<string> > applications; and then I want to write into it. applications...
7
by: Martin Magnusson | last post by:
I'm having trouble clearing and resizing a static std::vector of std::vectors (segmentation fault). Is it OK to call clear() and resize() on a static attribute? My code is similar to the one posted...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
0
by: nabh | last post by:
hi, i want to know how to declare multidimensional vectors in visual c++. ex: vector<vector<string>> variable; is this the way to declare a multidimensional vector variable?
5
by: asdf | last post by:
I was told not to use the low-level language such as arrays which inherited from C, I want to know what can I use to substitute the C-style multidimensional arrays? Is there multidimensional vector?
7
by: Markus Pitha | last post by:
Hello again, I have another question about the handling of multidimensional vectors. Actually, it's not difficult when the size is known at the beginning, but I still wasn't able to create...
1
by: Szabolcs Borsanyi | last post by:
The following code compiles with a warning message: passing arg 1 of `use_vector' from incompatible pointer type How can one correctly pass a multidimensional array (by reference) expressing...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
5
by: rottmanj | last post by:
I am trying to build a multidimensional vector array that allows for multiple child elements under each parent element, but I am having a heck of a time trying to figure out how to do it. Right...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.