desctructor of a vector

Feb 3, 2013 at 8:53am
Dear Cplusplus' users,

I create and use the following vector:

std::vector< bool > component_select(1);
component_select[0]=true;

And now I would like to use its destructor. I have tried different options such as:

~component_select();
or
~component_select;
or
component_select.~Vector();
or
std::vector< bool > ~component_select;

but nothing seems to work.

I would be really grateful if someone could help me.
Thanks in advance.
Best,
Isi.
Feb 3, 2013 at 9:32am
> And now I would like to use its destructor.

Why do you want to do that? The destructor would be invoked by the implementation when the life time of the object is over.

To remove all elements, use component_select.clear() ;
Feb 3, 2013 at 10:35am
Thank-you for your answer.

On one hand, and if I wanted to release memory and I wanted to eliminate it by myself because I knew when the life time of the object is over, couldn't I do it?

On the other hand, I wanted to use the destructor because I wanted to define it again with more elements, I mean:

std::vector< bool > component_select(1);
component_select[0]=true;
.
.
.

Using its destructor , that I don't know how to call it, I would do:

std::vector< bool > component_select(3);
component_select[2]=true;
.
.
.

Thanks in advance.
Best,
Isi.
Feb 3, 2013 at 10:49am
> I wanted to release memory and I wanted to eliminate it by myself

1
2
component_select.clear() ;
component_select.shrink_to_fit(); // C++11 

will (usually) release allocated memory.


std::vector<bool>().swap(component_select); will (always) release allocated memory.


> I wanted to define it again with more elements

Read this tutorial first: http://www.mochima.com/tutorials/vectors.html
Feb 10, 2013 at 9:33am
Dear JLBorges,

thank-you for your answer and your recommendation.

Best,
Isi.
Topic archived. No new replies allowed.