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

STL Vector - clear() works for 2D Vectors?

http://msdn2.microsoft.com/en-us/lib...ce(VS.80).aspx

vector <intv1;
v1.push_back( 10 ); //adds 10 to the tail
v1.push_back( 20 ); //adds 20 to the tail
cout << "The size of v1 is " << v1.size( ) << endl;
v1.clear( ); //clears the vector

I have a few questions:

Does clear() deallocates the memory too (like resize())?
Does clear() work for 2D vectors?
Or clear() is to be called for each dimension?

thanks in advance..

Nov 10 '06 #1
5 17967
madhu wrote:
http://msdn2.microsoft.com/en-us/lib...ce(VS.80).aspx

vector <intv1;
v1.push_back( 10 ); //adds 10 to the tail
v1.push_back( 20 ); //adds 20 to the tail
cout << "The size of v1 is " << v1.size( ) << endl;
v1.clear( ); //clears the vector

I have a few questions:

Does clear() deallocates the memory too (like resize())?
This is implementation defined. Same goes for resize().
Does clear() work for 2D vectors?
Or clear() is to be called for each dimension?
What do you mean by 2D vectors?

Ralpe

Nov 10 '06 #2

madhu wrote:
http://msdn2.microsoft.com/en-us/lib...ce(VS.80).aspx

vector <intv1;
v1.push_back( 10 ); //adds 10 to the tail
v1.push_back( 20 ); //adds 20 to the tail
cout << "The size of v1 is " << v1.size( ) << endl;
v1.clear( ); //clears the vector

I have a few questions:

Does clear() deallocates the memory too (like resize())?
As far as I'm aware of, both clear() and resize() does not perform any
de-allocation in terms of the memory allocated for the items. It only
calls the destructors of the items that were erased. A vector's
capacity (which is relative to the amount of contigious memory that it
represents) grows with amortized constant time as new items are added.
It never shrinks, unless you do this:

std::vector<Tnewv; //empty
oldv.swap( newv );

As far as 2D vectors are concerned, clear will erase all the items in
the first (or outer) dimension vector. This will cause destructors of
all items to be called, which effectively deletes all the unerlying
vectors - which of course erases the items that they contained, so YES.

R(r)esize will compare the current size, and erase items if excessive
items exist. If to little items exist, it may perform re-allocation,
causing all existing iterators to become invalid. This (invalidated
iterators) will obviously be the case for clear too.

Regards,

Werner
Does clear() work for 2D vectors?
Or clear() is to be called for each dimension?

thanks in advance..
Nov 10 '06 #3
madhu wrote:
http://msdn2.microsoft.com/en-us/lib...ce(VS.80).aspx

vector <intv1;
v1.push_back( 10 ); //adds 10 to the tail
v1.push_back( 20 ); //adds 20 to the tail
cout << "The size of v1 is " << v1.size( ) << endl;
v1.clear( ); //clears the vector

I have a few questions:

Does clear() deallocates the memory too (like resize())?
clear() doesn't necessary deallocate memory-- you don't need to worry
about this as it's the vector destructor's job to make sure that any
allocated memory is eventually freed. It does, however, invoke the
destructor of any object that gets cleared out of the vector.
Does clear() work for 2D vectors?
There's no such thing as a 2D vector (except in Physics class). What I
assume you're asking about is a vector of vectors and in this case, yes,
calling clear() does what you would expect it to: it invokes the
destructor of each of its contained vectors and, in the course of its
destruction, each of these vectors does the same for all of its
contained objects.

Nov 10 '06 #4

Mark P wrote:
What I
assume you're asking about is a vector of vectors and in this case, yes,
calling clear() does what you would expect it to: it invokes the
destructor of each of its contained vectors and, in the course of its
destruction, each of these vectors does the same for all of its
contained objects.

That's interesting..
But does this mean that a single call would remove all the objects in
all the dimenstions?
Or it is to be done iteratively for each dimention?

I well could have a vector of a vector of a vector (a.k.a. 3D).. or
maybe even higher.

- Divya Rathore
(remove underscores for email ID)

Nov 10 '06 #5
di************@gmail.com wrote:
Mark P wrote:
> What I
assume you're asking about is a vector of vectors and in this case, yes,
calling clear() does what you would expect it to: it invokes the
destructor of each of its contained vectors and, in the course of its
destruction, each of these vectors does the same for all of its
contained objects.


That's interesting..
But does this mean that a single call would remove all the objects in
all the dimenstions?
Or it is to be done iteratively for each dimention?

I well could have a vector of a vector of a vector (a.k.a. 3D).. or
maybe even higher.
Only one call is needed to start the recursive process.

Clearing or destructing a vector will destruct all of its contents. If
those contents are vectors then destructing any of those vectors will
destruct all of its contents. If those contents are vectors then
destructing any of those vectors will destruct all of its contents. If
those contents are vectors... get the idea?
Nov 10 '06 #6

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

Similar topics

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...
1
by: Dennis | last post by:
Hi I'm trying to implement a vector of vectors where find can be used to find a vector<double> in the vectors of vectors, that is hard to understand i guess. What I mean is that I got a vector...
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...
1
by: robk | last post by:
Hi, Could someone know what is wrong with my code. First of all what I'm trying to do. I have (as can be seen) declared typedef's of vector STL. I want each value in vector which is part of...
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
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"...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
0
by: hanthehead | last post by:
In order to use a C# dll from our unmanaged C++ project I have created a mixed managed\unmanaged c++ dll. This dll is passed an unmanaged class which contains vector<CDBRecord> m_vecRecords member...
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...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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...
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...
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...

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.