STL: shrinking of vector 
Author Message
 STL: shrinking of vector

Hi,

is there a possibility to shrink a <vector>? The only way I know so far
is to delete it.

cu
Olaf



Tue, 30 Jul 2002 03:00:00 GMT  
 STL: shrinking of vector


Fri, 19 Jun 1992 00:00:00 GMT  
 STL: shrinking of vector
Derive a class from vector and override clear, or write a "pack()" method.
In these methods you can just clear the allocator, or copy the data and
reallocate the allocator.

Sytse.

Quote:

>is there a possibility to shrink a <vector>? The only way I know so far
>is to delete it.



Tue, 30 Jul 2002 03:00:00 GMT  
 STL: shrinking of vector


Fri, 19 Jun 1992 00:00:00 GMT  
 STL: shrinking of vector

Quote:

>Hi,

>is there a possibility to shrink a <vector>? The only way I know so far
>is to delete it.

If you're talking about releasing the newly unused part of the
vector's element storage when you erase elements or resize() the
vector, then no. If you want to "reset" the vector, you can swap it
with an empty temporary vector, e.g.

{
   vector<int> t;
   v.swap(t);

Quote:
}

If you want to reduce a vector's storage to the minimum possible,
copying it to 't' before swapping should do the trick.

--
Doug Harrison

Visual C++ MVP



Tue, 30 Jul 2002 03:00:00 GMT  
 STL: shrinking of vector


Fri, 19 Jun 1992 00:00:00 GMT  
 STL: shrinking of vector
I believe publicly deriving a class would be a bad idea. Since vector<> has
no virtual destructor, the result of deleting your derived class using a
base class pointer is undefined.

_____
Barry L. Wallis, Senior Systems Engineer
Science Applications International Corporation
4161 Campus Point Court
San Diego, CA 92121
  _____
"In all labor there is profit, but mere talk leads to poverty."
- King Solomon


Quote:
> Derive a class from vector and override clear, or write a "pack()" method.
> In these methods you can just clear the allocator, or copy the data and
> reallocate the allocator.



Wed, 31 Jul 2002 03:00:00 GMT  
 STL: shrinking of vector

Quote:
> I believe publicly deriving a class would be a bad idea. Since vector<>
has
> no virtual destructor, the result of deleting your derived class using a
> base class pointer is undefined.

    But is that something you are likely to do?  Have you ever felt the need
to allocate a vector<> with new?  I haven't.

    Further, the changes recommended should not involve adding any data
member, not requiring any destruction beyond what is offered in the base
class dtor.  So, while it may be officially undefined, it's almost certainly
safe.

    Sometime you just have to acknowledge the difference between "writing
robust code" and "pointlessly tying your hands".

--
Truth,
James Curran
http://www.NJTheater.com
http://www.NJTheater.com/JamesCurran



Fri, 02 Aug 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. JPG image is cropped not shrunk -size shrank

2. STL vector casting

3. There is any equivalent for Vector Container(STL) in C#

4. Memory Leak with vector STL

5. How to deallocate CComObject's inside a STL vector

6. STL Vector in Watch Window?

7. STL Vectors, iterators

8. STL Vectors, iterators

9. STL vector element display question.

10. how to watch/debug STL vectors ans lists

11. Clever way to encapsulate STL vector?

12. debugging STL vectors

 

 
Powered by phpBB® Forum Software