473,387 Members | 1,834 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,387 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 18039
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.